As reported: Cohesity MTS-2, 2026, round 1 (DSA): design and implement a key-value store with LRU eviction.
This is LeetCode 146 — LRU Cache. Our write-up: /lru-cache.
Implement the LRUCache class:
LRUCache(int capacity)initialises the cache with a positive capacity.int get(int key)returns the value ofkeyif it exists, otherwise-1. A successfulgetmakes the key the most recently used.void put(int key, int value)updates the value ifkeyexists (and makes it most recently used); otherwise inserts it. If the insert makes the number of keys exceedcapacity, evict the least recently used key.
get and put must each run in O(1) average time.
Example
["LRUCache","put","put","get","put","get","put","get","get","get"]
[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]
Output: [null,null,null,1,null,-1,null,-1,3,4]
After get(1), key 2 is the least recently used, so put(3,3) evicts 2. Then put(4,4) evicts 1.
Constraints: 1 <= capacity <= 3000, 0 <= key <= 10^4, 0 <= value <= 10^5, at most 2 * 10^5 calls to get and put.
Code runner format: two lines, LeetCode's design format: the method names, then the argument lists. The output lists each call's result (null for the constructor and put).