iq.lab
Python starts when a code cell comes near or you run one

Module 5 · 3 lessons · about 5 hours 40 minutes

Stacks and queues

Stacks for matching and nesting, monotonic stacks for the next bigger thing, and queues and deques for processing in arrival order.

Lessons
0/3
Exercises
0/23
Code
0/3
Problems
0/15
Checkpoint
Not taken

Already comfortable with this? Take the checkpoint cold. If you pass, move on and come back only for what you missed.

  1. 1StacksA stack is a list you only touch at its end, so the last item in is the first one out. It is the notebook whenever the newest unfinished item is the one you need next, from open brackets to numbers waiting for an operator.2 h 5 min
  2. 2Monotonic stacksFor each item, find the next bigger item to its right. Scanning right from every item is O(n²). One walk with a stack of items still waiting for an answer does it in O(n), and the same stack finds the largest rectangle under a bar chart.1 h 50 min
  3. 3Queues and dequesA queue hands back the oldest item first. Python's deque adds and removes at both ends in O(1), so it is the notebook for work in arrival order, and, kept in falling order, it finds the largest number in every window in one pass.1 h 45 min
  4. Checkpoint11 questions. Pass with 80% to finish the module.

Problems for this module

The lessons teach some of these step by step (each says which). The rest are practice: try each in its target time, and solved problems come back for spaced re-solves.

Next moduleSorting and binary search