Skip to main content

Pattern · 15 problems

Stack Interview Problems

A stack remembers unfinished business. I push what is waiting and pop when something arrives that settles it.

Get the link for your laptop

Free: write the code and run it, no sign-up.

When I reach for it

  • Things must close in the reverse order they opened.
  • You need the nearest earlier or later item that is bigger or smaller.
  • Nesting, undo or parsing shows up in the statement.

Problems, easiest first

2 easy · 9 medium · 4 hard. Open one to read it, then write it in the free editor.

The idea

Ask what is waiting. Brackets wait for a closer. A cold day waits for a warmer one. The stack holds those waiting items, newest on top.

For nearest-greater problems, keep the stack in order. Pop every item the new one beats, answer for each, then push the new item.

pairs = {")": "(", "]": "[", "}": "{"}
stack = []
for ch in text:
    if ch in pairs:
        if not stack or stack.pop() != pairs[ch]:
            return False          # closer found no matching opener
    else:
        stack.append(ch)          # opener waits for its closer
return not stack                  # anything left is unclosed
Matching brackets. A closer must find its opener on top, and nothing may be left over at the end. Python shown; the idea carries to every language in the editor.

Where it goes wrong

  • Popping from an empty stack.
  • Forgetting the leftovers after the loop.
  • Pushing values when the answer needs distances. Push indexes instead.

Say this before you type. Say what each item on the stack is waiting for, and what lets you pop it.

Where Coco comes in

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.

Questions people ask

How do I know a stack fits?
Look for last-in, first-out behavior. The newest unfinished thing is always the next one to settle.
What is a monotonic stack?
A stack kept in sorted order by popping items the new one beats. It finds the next greater item for every position in one pass.

Meet these next

Fin and Coco are characters, not people. strongyes.io is independent. Leo builds it with AI agents.