Card 06/ 06

RecapDifficulty: Intermediate2 min

Recap: Combining Data Structures

Name the operation each structure is slow at, let the other cover it, and update both every time.

Combining data structures means keeping the same items in two structures, so that each covers the operation the other is slow at. The topic comes down to the five ideas below, each listed with the card that covers it. One rule links them: two structures over the same items are one structure, so every change goes to both.

What the topic established, and which card it was on
The ideaWhere
An LRU cache pairs a hash map, which finds a key, with a doubly linked list, which keeps the order of use; the map stores a reference to each nodeCombining Two Structures to Cover Each Other's Slow Operation
Every get or put moves its key to the front, and a full cache evicts the key at the back from both structuresTrace an LRU Cache Through Gets and Puts
Sentinel nodes remove the edge cases; OrderedDict, an access-ordered LinkedHashMap and a re-inserted Map are the built-in versionsLRU Cache in Python, Java and TypeScript
An array and a map of indexes pick a random item in O(1), and delete by moving the last item into the gapCombining Example: A Set That Picks a Random Item in O(1)
A change to one structure but not its partner fails later, so one method updates bothUpdating One Structure but Not Its Partner

The cache's costs show the method at work: each structure alone fails one operation, and the pair makes all three O(1).

The LRU cache's operations in each structure alone and in both
OperationHash map aloneDoubly linked list aloneBoth together
Get the value for a keyO(1) on averageO(n)O(1)
Mark a key as most recently usedNot possibleO(1) once foundO(1)
Evict the least recently used keyO(n)O(1)O(1)

The same method applies beyond these two designs: write down the operations, find the one each candidate structure is slow at, and look for a partner that makes it cheap. Together these ideas let you design a structure from two parts when no single one fits, trace both parts through any sequence of operations, and keep them in step so that neither tells a different story from the other.