</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Stack Fundamentals

What is this?

Think of a stack as a pile of plates: you only ever add to the top or take from the top. There are just three things you can do โ€” put a plate on (push), take the top one off (pop), and peek at the top one โ€” and each is instant. Once you have those three moves, the classic first puzzle is checking that brackets like ( ), [ ], and { } open and close in the right order.

flowchart TD A["See an opener"] --> B["Push it on the stack"] C["See a closer"] --> D["Check the top matches"] D --> E["Pop the top off"] F["Reached the end"] --> G["Stack should be empty"]

๐Ÿ’ก Fun fact: Text editors track every change on a stack so that Undo always reverses your most recent edit first โ€” pop the last action and the document steps back in time.

๐Ÿ”“ The 1 problem in this chapter is free. Sign in with Google or Microsoft to start solving.


Core idea: A stack is a pile you touch only at the top โ€” last in, first out (LIFO). Master the three O(1) moves (push, pop, peek) and the one pattern that defines this chapter: push things as you meet them, and resolve the most recent one when its match arrives.


The three operations, and nothing else

Chalkboard diagram of a stack with cells a, b, c and the three O(1) operations push, pop, peek with a top pointer

All three are O(1) โ€” no shifting, no searching, you only ever touch the top. In Python a list already is a stack: stack.append(x) to push, stack.pop() to pop, stack[-1] to peek, if not stack to test empty.

The defining property: a stack hands items back in reverse order of arrival. So whenever "the next thing to deal with" is "the most recent thing I haven't finished," the stack already has it waiting on top.


When a problem is secretly a stack

  • Nesting / matching โ€” brackets, tags, BEGIN/COMMIT โ€” the innermost (most recent) must close first.
  • Undo / backtracking โ€” the last action is the first undone.
  • Deferred work โ€” pause the current task, handle a nested one, resume โ€” exactly what the call stack does.

If you can phrase the task as "the latest unresolved item gets resolved first," reach for a stack.


The problem in this chapter

Valid Parentheses โ€” the LIFO pattern in its purest form

Given a string of ()[]{}, decide if every bracket closes in the right order. Push each opener; on a closer, the top of the stack must be its matching opener (pop it) โ€” otherwise it's invalid. A valid string ends with an empty stack.

Chalkboard trace of Valid Parentheses pushing and popping while scanning ( [ ] ) ending with an empty stack

Two ways to fail, and both matter: a mismatch (closer doesn't match the top) and leftover openers (stack not empty at the end). โ†’ O(n) time, O(n) space.


The cross-cutting skill: think in pushes and pops

Situation Stack move
See an opener / a new unresolved item push it
See a closer / a resolver check the top, then pop
Reach the end the stack should be empty (or its remainder is the answer)
Stack empty when you need to pop the input is malformed โ†’ handle it

A stack turns "is the most recent unresolved thing the right one?" into a single peek + pop. Get comfortable here โ€” every later chapter (expression eval, paths, monotonic stacks) is a variation on this move.


๐Ÿ““ Draw it yourself

  1. Push/pop trace. Draw a vertical box and walk push a, push b, pop, push c, pop, pop, redrawing the pile each step. Always touch the top.
  2. Brackets as a stack. Take ([{}]) and draw the stack growing on each opener and shrinking on each closer; circle the moment the top must match the incoming closer.

Snap photos and embed them with the /host-diagrams skill.


Complexity at a glance

Operation / problem Time Space
push / pop / peek O(1) โ€”
Valid Parentheses O(n) O(n)

Key takeaways

  • LIFO, three O(1) moves. Push, pop, peek โ€” you only ever touch the top.
  • In Python, a list is a stack (append, pop, [-1], not stack).
  • The fundamental pattern: push the unresolved, pop to resolve, and check the stack is empty at the end.
  • Guard your pops โ€” popping an empty stack is the classic bug.
  • Why this chapter matters: every advanced stack problem โ€” expression evaluation, path simplification, monotonic stacks โ€” is built from exactly these moves.

Next: Valid Parentheses, then carry the push/pop instinct into Expression Evaluation.

Parentheses Validation

Core idea: Push every opener onto a stack; when a closer arrives, it must match whatever is sitting on top โ€” because the bracket you opened most recently is the one you must close first.

Problem, rephrased

Forget brackets for a second. Picture a warehouse worker stacking nesting crates. Each time a crate is opened it goes on top of the pile, and the worker can only seal whichever crate is currently on top. A round crate must be sealed with a round lid, a square crate with a square lid, a curly crate with a curly lid. At the end of the shift the pile must be completely empty โ€” nothing left open, nothing sealed with the wrong lid, and no lid showing up before its crate was ever opened.

Now swap "crate" for an opening bracket and "lid" for a closing bracket. You're handed a string made only of (, ), [, ], {, }. Return True if and only if every bracket is closed by the matching type, in the correct order, with nothing left over.

Input Output Why
"()" True One pair, opened then closed
"([])" True Square nested cleanly inside round
"(]" False Round opener sealed with a square lid (mismatch)
"([)]" False Interleaved โ€” the ) tries to close [
"(((" False Three crates left open at end of shift
"))" False A lid appears with no crate underneath it
"" True Nothing to validate, trivially balanced

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.