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.
- 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
- 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
- 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
- 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.
- easyValid parentheses15 min
- mediumMin stack25 min
- mediumEvaluate reverse Polish notation25 min
- mediumDecode string25 min
- mediumRemove the fewest parentheses25 min
- mediumAsteroid collision25 min
- easyNext greater element15 min
- mediumDaily temperatures25 min
- hardLargest rectangle in a histogram40 min
- mediumOnline stock span25 min
- easyQueue from two stacks15 min
- hardSliding window maximum40 min
- easyRecent calls counter15 min
- hardBasic calculator40 min
- mediumLongest run within a limit25 min