← Whiteboard questions

Pattern

Stack

Use a stack when the most recent unfinished thing must be dealt with first, as in matching brackets, undoing steps or finding the next larger item. Keep only what is still waiting. Each item is pushed and popped once, so the cost is usually O(n).

Stack interview questions and solutions