Level 1 Plain LRU
Build Cache(capacity), where capacity >= 1:
get(key)returns the stored value, orNoneon a miss. A hit makes the key the most recently used.put(key, value)inserts or overwrites the key and makes it the most recently used. If the cache now holds more thancapacitykeys, it drops the least recently used one.
⭐ Bonus: a speed test earns a star if both are O(1). OrderedDict is allowed, but writing the structure yourself is better interview practice. Keys are hashable and values are anything except None.
c = Cache(2)
c.put("a", 1); c.put("b", 2)
c.get("a") # 1 ("b" is now least recent)
c.put("c", 3) # drops "b"
c.get("b") # None