Daily iOS
CS fundamentals and algorithms

Balanced brackets

A Swift string mixes ()[]{} and you're checking with a stack whether they nest correctly. When you hit a closing bracket, what comparison do you make?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Push opening brackets onto the stack; when a closing bracket appears, pop the top and check that it's the opening bracket of the same kind. If the stack is empty or the kinds differ, return false right there.

Share a question