Ten problems that drill the design-a-data-structure pattern from Lesson 27. Each one is really a question of “which structures do I combine, and how do I keep them in sync?” — decide that first, then the code follows. Every solution here was actually run and tested.
How to practise. Before writing anything, say out loud which operations need to be fast and which structure gives you each one. Then implement from a blank page, run your own small sequence of operations against it, and only then compare with the solution. These are bookkeeping problems — the bugs come from one structure drifting out of sync with the other, so trace an op through both structures.
The 10 problems
- 1Design HashMap (LeetCode 706)EasyBuild a hash map without built-ins: an array of buckets indexed by key % size, each a list of key-value pairs.arrow_forward
- 2Min Stack (LeetCode 155)MediumDesign a stack with O(1) getMin: store each value together with the minimum of everything below it.arrow_forward
- 3LRU Cache (LeetCode 146)MediumBuild a fixed-size cache that evicts the least recently used key: a hash map plus a recency-ordered list.arrow_forward
- 4Insert Delete GetRandom O(1) (LeetCode 380)MediumO(1) insert, delete and getRandom: an array plus a value→index map, with swap-with-last on delete.arrow_forward
- 5Design Circular Queue (LeetCode 622)MediumA fixed-capacity queue on an array: track head and count, and wrap indices with modulo so nothing ever shifts.arrow_forward
- 6Design Twitter (LeetCode 355)MediumMini Twitter: per-user tweets with a global timestamp, a follow graph, and a heap for the 10 newest.arrow_forward
- 7Snapshot Array (LeetCode 1146)MediumAn array with snapshots: keep each index's (snap_id, value) history and binary-search it; never copy.arrow_forward
- 8Design Underground System (LeetCode 1396)MediumTrack metro journeys: a map of open check-ins plus a running (total, count) per route makes every query O(1).arrow_forward
- 9Product of the Last K Numbers (LeetCode 1352)MediumProduct of the last k stream numbers in O(1): keep prefix products and reset them when a 0 arrives.arrow_forward
- 10LFU Cache (LeetCode 460)HardO(1) LFU cache: bucket keys by use count, keep buckets in recency order, and track the minimum count.arrow_forward