Data structures for LLD 00:00:00 Make a prediction You will derive a data-structure choice from the operations that must be fast, and state the extra invariant when you combine two structures. A two-entry cache stores tea, then milk. Somebody reads tea and then adds bread. Which item should leave if we evict the least recently used? Milk leaves. Reading tea refreshed its position. A map finds tea by key, while an ordered list records recency. Removing the same milk entry from both structures keeps the cache consistent. 00:00:34 Noor's shed A tool shed has a labelled drawer index and hooks ordered by the last time a tool was used. The index finds a tool quickly, but only the hooks tell you which tool has waited longest. Each structure answers a different question. When you move or remove one tool, update both records. A dangling drawer reference is exactly the kind of error that two-structure designs must test. In a design round a data structure is a private decision inside a class. The public methods say what can be asked. The container decides how long an answer takes, in which order things come out, and what can go wrong when two threads ask at once. Interviewers ask why this structure in almost every round. The answer is a call and its cost, not a favourite: take the nearest free spot, many times a minute, so a heap. 00:01:27 Find the oldest cache entry A cache of size two contains tea and milk. Finding tea quickly suggests a hash table; choosing the least recently used item also needs order. Let the table store references to list nodes. Reading tea moves its node to the front without scanning for it. Insert bread. Milk is now the tail, so remove its node and table entry. Both data structures must still describe the same keys. After reading tea, put juice into the full cache. The victim is milk because tea was just touched. A map still holding milk after the recency node was removed would return an evicted value. Check both indexes after replacement, including an overwrite that leaves the count unchanged. 00:02:13 Pick a different structure for deadlines For a scheduler, a hash table makes lookup by identifier cheap but cannot cheaply reveal the next deadline. A minimum heap exposes the earliest deadline. If cancellation matters too, keep an identifier index or mark cancelled jobs and discard them when they reach the top. Try canceling task A while it is at the heap root. A lazy implementation marks its ticket invalid; the next retrieval discards A and returns B. Repeat with B canceled below the root. There is no need to scan immediately, but every retrieval must recognize a stale ticket. A hash table supports fast lookup, but iterating all keys to find the oldest entry defeats the cache's intended eviction cost. For a scheduler that only needs the earliest deadline, a minimum heap is a better fit. Add another index only if lookup or cancellation requires it. Redis sorted sets: two structures in one. The documentation says sorted sets are implemented with a skip list and a hash table together. One gives order, the other gives the score of a member in one step. Java LinkedHashMap. Keeps insertion order, or access order if asked in the constructor. The class page says that the second kind is well suited to building LRU caches. 00:03:38 Try an interview follow-up When would you refuse a clever structure? I would decline the extra index when the input and call frequency make its benefit tiny. Suppose we have thirty items and two requests a minute. A plain loop checks at most thirty entries each time. A second container would add a consistency rule to every write, with little measurable saving here. A catalogue shows its cheapest item and now must remove a product by identifier. Must an identifier lookup replace the structure that orders prices? Keep a representation that can answer each required operation, or accept a scan if deletion is rare. If you keep two indexes, demonstrate how removing one product updates both, including the case of two products with the same price. 00:04:30 What to take with you List the calls, name the question of each, and let the question pick: map for a key, sorted map for a range, heap for the smallest, deque for first in first out, stack for last in first out, bit set for many flags, linked map for order plus lookup. Two fast questions mean two containers, one place that changes both, and an invariant you can say aloud.