LRU Cache

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Write a class LRUCache, made with new LRUCache(capacity). get(key) returns the value stored for key, or -1 if there isn't one; put(key, value) stores it, replacing any old value. When a new key would go over capacity, first drop the least recently used key: the one that has gone longest without a get or a put. Both should take constant time; the checks make 100,000 calls.

Your code must pass

  • Stores and returns values
  • Returns -1 for keys it doesn't have
  • Drops the least recently used key when full
  • Counts a get or a put as a use
  • Is fast enough for 100,000 calls