Level 2: Data Structure Algorithms Exercises
Level 2 is about choosing the right container. The same job can take a second
or an hour depending on whether your data sits in a list, a deque, a dict, a
heap or a tree. Every exercise is a task from a real codebase: schedule jobs
fairly, find the shortest route through a warehouse, merge overlapping
bookings, work out how many meeting rooms a day needs.
As in Level 1, some tests feed your code thousands of items and count its steps, and two of them check that your code survives inputs deeper than Python's recursion limit. The LRU Cache project combines a hash map with recency order, the structure behind every web cache.
The 10 exercises in this level
- 1 Queues with deque Round-robin job scheduling Schedule jobs fairly with a first-in, first-out queue: each job runs for a time slice and goes back in line until it's done, with deque instead of a slow list.pop(0). Start here — no prerequisites
- 2 Reversing a Linked List Turn every arrow around, in place Reverse a linked list by relinking its nodes in one pass, without copying them, and without recursion that breaks on long lists. Start here — no prerequisites
- 3 Anagram Groups with a Dict Group words by a computed key Group the words of a word game that use the same letters in one pass, with a dict keyed by the sorted letters instead of comparing every pair. Start here — no prerequisites
- 4 Shortest Path with BFS Fewest steps through a warehouse Find the fewest steps from a start to a goal through a warehouse map with breadth-first search, which explores in rings so the first arrival is the shortest. Recommended first: Queues with deque
- 5 Counting Regions with DFS Separate zones on a floor map Count the separate zones of a floor map with depth-first search and your own stack, so a long corridor doesn't hit Python's recursion limit. Recommended first: Shortest Path with BFS
- 6 Merging Intervals Combine overlapping bookings Merge overlapping and back-to-back room bookings with one sort and one pass, and keep a long booking whole when a short one sits inside it. Start here — no prerequisites
- 7 Heaps: Meeting Rooms How many rooms does the day need? Work out how many meeting rooms a day needs with a heap of end times, so the room that frees up first is always on top. Recommended first: Merging Intervals
- 8 Binary Trees, Level by Level Read a tree one row at a time Read a binary tree row by row, top to bottom, with a queue and one count per round that keeps the rows apart. Recommended first: Queues with deque
- 9 Binary Search Trees Check that a price index is still sorted Check that a binary search tree is still valid by passing the allowed range down the tree, where comparing a node with its children is not enough. Recommended first: Binary Trees, Level by Level
- 10 Project: LRU Cache Keep the most recent API answers Build the cache that sits in front of most APIs: keep the most recently used answers, evict the oldest in O(1), and count a read as a use. Recommended first: Reversing a Linked List, Anagram Groups with a Dict
Frequently asked questions
- What should I know before Level 2?
- Level 1 of this course, or the same ideas: Big-O, dicts and sets, two pointers and binary search. You should also be able to write a small class with an __init__ method.
- Why do some tests fail with a RecursionError?
- Python stops recursion after about 1,000 nested calls, and less in the browser. Real data, like a long linked list or a big map, goes deeper. Those exercises teach you to replace recursion with your own stack or a loop.
- Are these data structures used outside interviews?
- Constantly. Queues run job schedulers, heaps power calendars and priority queues, BFS finds routes, trees index databases and an LRU cache sits in front of most APIs.
- Do I have to implement heaps and deques myself?
- No. You use Python's collections.deque, heapq and OrderedDict, as in real code. The exercises are about knowing which one fits and what each operation costs.