DSA Factory
All free lessons
Level 2 · Core

Free Stacks and queues lessons

12 lessons, about 162 minutes in all. Each teaches one idea with a visual, then gives you one problem built for it. Read the lessons here, then create a free account to solve the problems and keep your progress.

Stage 0Last in, first out

  1. An undo buttonThe newest item comes off first.8 min
  2. Typing with backspaceBuild text with a stack; backspace pops the last character.8 min
  3. Brackets that matchThe newest open bracket must close first.10 min
  4. A queue: first in, first outJoin at the back, leave from the front.10 min

Stage 1Matching brackets

  1. How many brackets are missingCount the fewest brackets to add so a row of round brackets matches, noticing that a stack of identical brackets is just a counter.12 min
  2. The longest run that matchesFind the longest stretch of a bracket string that is balanced, by keeping the positions of the unmatched brackets on a stack.16 min
  3. Score the bracketsScore a balanced bracket string where a pair holding nothing is worth 1, a pair around something doubles it, and neighbours add, using a stack of partial scores.16 min
  4. Reverse inside the bracketsReverse the text inside each pair of brackets, from the innermost outward, by keeping one piece of text per open level on a stack.16 min

Stage 2Monotonic stack

  1. The next bigger value on the rightFor every item, find the first value to its right that is bigger, by keeping a stack of items still waiting for something bigger.14 min
  2. Days until a warmer oneFor each day, count how many days you must wait for a warmer temperature, by keeping the days still waiting on a stack and answering them with distances.14 min
  3. The biggest rectangle in a bar chartFind the largest rectangle that fits under a bar chart, by popping each bar when a lower bar arrives and measuring how far it could stretch.20 min
  4. The smallest number after removing digitsRemove exactly k digits from a number to make it as small as possible, by dropping any digit that is larger than the digit that follows it.18 min

There’s more after this

These are the opening stages of Stacks and queues. The full path climbs on to harder problems and a mastery test. Create a free account and we’ll keep your place.

Start the full path free