10/37
Part 2 of 6 · The LRU orderLearn and do

A put has three cases, and only a new key in a full cache evicts

  • The key is stored. Set the new value and move its node to newest. Never evict: the cache isn't growing.
  • A new key, and the cache is full. Take head.next, the oldest, off the list, and delete its key from the map. Then add the new key, as in the next case.
  • A new key. Make a node, map the key to it, and push it back.

Here is the full case, as the trace's put("c", 3) arrives:

Predict

Evicting takes head.next off the list, then deletes its key from the map. Where does it get that key?

Pick an answer above to see the rest