Stacks

Last-in, first-out collection for undo, parsing, and nested work.

Stacks Practice Problems

Easy

4 problems
  1. 340

    Valid Parentheses

    Use a stack to check that every opening bracket is closed by the matching closing bracket in the right order.

    easy
  2. 341

    Min Stack

    Build a stack that also reports its smallest value instantly, by storing a running minimum beside every pushed value.

    easy
  3. 342

    Baseball Game

    Keep a score record on a stack, where each operation records, cancels, doubles or sums the most recent scores.

    easy
  4. 343

    Remove All Adjacent Duplicates In String

    Cancel equal neighbouring letters in one pass by popping the stack whenever the next letter matches the top.

    easy

Medium

4 problems
  1. 344

    Evaluate Reverse Polish Notation

    Evaluate an expression where operators come after their numbers, by pushing numbers and folding each operator into one result.

    medium
  2. 345

    Decode String

    Expand nested k[text] patterns using two stacks that park the repeat count and the text built before each bracket.

    medium
  3. 346

    Asteroid Collision

    Track surviving asteroids on a stack, where each left-moving asteroid fights the top until one of them is destroyed.

    medium
  4. 347

    Simplify Path

    Turn a Unix file path into its canonical form by pushing folder names and popping one off for every ".." part.

    medium

Hard

2 problems
  1. 348

    Basic Calculator

    Evaluate a sum with brackets by parking the running result and the current sign on a stack whenever a bracket opens.

    hard
  2. 349

    Largest Rectangle In Histogram

    Find the biggest rectangle under a row of bars with a monotonic stack that measures each bar as soon as a shorter one arrives.

    hard