Pergunta de entrevista da empresa MongoDB
Given a string of parantheses, brackets, and curly braces, write a function that returns whether the string is well balanced, in that every left delimiter is closed by the correct right delimiter. I was asked this question by multiple interviewers, it seems like their go-to question.
Respostas da entrevista
I would use a stack to store each open brace as it is encountered. When a close brace is found, I would compare it with the previous open brace. If they do not match, reject the string. If they match, pop the open brace off the stack and continue parsing the string.
second answer is sooooooo wrong
Through questions like this, interviewers are mostly trying to test your skillset (and its relevance to the role) as robustly as possible, so be prepared for multiple offshoots and followups.
It could be a useful exercise to do mocks with friends or colleagues in MongoDB to get a real sense of what the interview is actually like. Alternatively Prepfully has a ton of MongoDB Software Engineering Intern experts who provide mock interviews for a pretty reasonable amount.
prepfully.com/practice-interviews
Maintain a counter variable (an integer) for each of the 3 kinds of brackets / braces etc. Whenever you encounter an open bracket / brace , increment its counter. If you encounter a close, decrement the counter. At the end of the loop check if the count is negative - in this case you have an invalid ordering. At the end of the loop if all the counters are 0, you're parenthesis string is valid.