Command Palette

Search for a command to run...

Module 40

Designing Data Structures

"Design X" problems: combine hash maps, linked lists, heaps and sorted maps to hit O(1) or O(log n) per operation. LRU, LFU, time-based stores, snapshots and feeds.

Advanced 2 lessons 13 problems ~30 min of lessons

Design problems give you a list of operations and a speed requirement, and ask for a class. No single structure fits, so the answer is a combination: a hash map for O(1) lookup glued to a linked list for O(1) ordering, a map of sorted lists for history, or a heap for merging feeds.

This module teaches a repeatable method (list operations → required complexity → pick a structure per need → keep them consistent) and applies it to the classic designs asked in interviews and used in real caches and services.

Best after: Hashing, Linked Lists, Heaps and Priority Queues

Unlocks next: 41. Concurrency-Aware Data Structures

Where this shows up in real systems

Part 1

Learn the ideas

Part 2

Solve the problems

Work through them in order. Each one shows the pattern it teaches.

  1. HashMap + doubly linked list with sentinels.

  2. Frequency buckets with a minimum-frequency pointer; ties broken by recency.

  3. Append-only versions per key plus binary search for the latest version at a time.

  4. Store only changes per index, tagged with the snapshot id; answer with a floor lookup.

  5. Array + index map, deleting by swapping with the last element.

  6. A sliding time window with a queue, or a fixed ring of 300 buckets for O(1) memory.

  7. A news feed is a K-way merge of followed users' tweet lists, newest first.

  8. What's inside a hash set: an array of buckets, a hash function to pick the bucket, and a small list per bucket for collisions.

  9. A hash map is a hash set whose bucket entries carry a value: update in place when the key is already there.

  10. Two views of the same items: a doubly linked list for stack order and a TreeMap for "largest", kept in sync.

  11. Paths as keys: a parent must exist before its child, which is a prefix check (map of full paths, or a trie of path parts).

  12. Autocomplete: keep the typed prefix, find matching past sentences, rank by popularity then alphabet, return the top 3; '#' saves the sentence.

  13. Two maps: who is currently travelling (id → start), and running totals per route ("from→to" → sum and count).