Build a Typed LRU Cache Class
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write a class LruCache<K, V> holding at most capacity entries (given to the constructor). get(key) returns the value, or undefined if it isn't there, and counts as a use; set(key, value) adds or replaces an entry, and when that takes the cache over capacity, it drops the least recently used entry. size is the number of entries. Type it so keys and values must be K and V, and get returns V | undefined.
Your code must pass
- Returns stored values, and undefined for missing keys
- Drops the least recently used entry when full
- Reading or replacing an entry counts as a use
- Has no type errors
- get returns V or undefined
- Rejects keys and values of the wrong type