AlgoViz

Design an LRU cache

medium 85 min

Design an in-memory Least Recently Used cache with O(1) lookups, updates and evictions.

Watch lesson4:49

What each level should show

The same question is judged differently for mid, senior and staff.

The same question at four levels

  • Junior

    Must show

    • Both structures with reasons
    • Working get and put
    • A trace with an eviction

    Will be probed on

    • Why doubly linked?
    • Why the key in the node?

    Stands out

    Writes unlink and push first and never touches pointers again.

  • Mid

    Must show

    • Sentinels, clean split into list and cache
    • The update case on a full cache
    • One lock, and why a read lock fails

    Will be probed on

    • Time to live
    • Evict before or after

    Stands out

    States the invariant and checks it at each line.

  • Senior

    Must show

    • Shards and buffered reads with their price
    • Scan resistance
    • Single flight with errors and invalidation

    Will be probed on

    • How much is under your lock?
    • How would you size it?

    Stands out

    Does the sum before splitting the lock, and asks whether exact order is needed.

  • Staff

    Must show

    • Policy interface that stays O(1), and admission
    • Memory of an entry, layout
    • Resource values that must not be closed in use

    Will be probed on

    • What do you measure first?
    • When would you remove the cache?

    Stands out

    Replays a trace before choosing a policy, and can say what the cache may get wrong and for how long.