Stack Problems for Coding Interviews

A stack fits any problem with nested or most-recent-first structure: matching brackets, evaluating expressions, undo history and parsing. The monotonic stack finds the next greater or smaller element in linear time.

Look for the phrase next greater, previous smaller or nearest valid. Practise explaining what the stack holds at each step and why elements are popped, and bound the total work by showing each item is pushed and popped once.

Coding problems

Related

Frequently asked questions

How do I get good at stack problems?

Solve a few problems of each difficulty, name the pattern after you solve each one, and revisit the ones you needed hints for a week later. The list on this page is ordered so you can start with the basic ones.

Can I run stack problems online here?

Yes. Every problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and six more languages, with no setup.