As reported: Intuit SWE intern (on-campus, 2026), technical interview 2: implement an LRU cache from scratch, including a
mainto run it, then debug it against the interviewer's test cases.
Implement an LRUCache class:
LRUCache(capacity)creates a cache holding at mostcapacitykeys.get(key)returns the value forkey, or-1if it is absent. A successfulgetmakes the key the most recently used.put(key, value)inserts or updates the key and makes it the most recently used. If the insert pushes the cache over capacity, evict the least recently used key first.
Both operations must run in O(1) average time.
This is LeetCode 146 — LRU Cache.
Example
LRUCache(2)
put(1,1), put(2,2), get(1) -> 1, put(3,3) evicts key 2
get(2) -> -1, put(4,4) evicts key 1
get(1) -> -1, get(3) -> 3, get(4) -> 4
Constraints: 1 ≤ capacity ≤ 3000; 0 ≤ key ≤ 10^4; 0 ≤ value ≤ 10^5; up to 2·10^5 calls.
Code runner format: one JSON value per line (LeetCode's test-case format): the list of operation names, then the list of their arguments. The harness prints the results as a JSON list (null for the constructor and put).
Clarifications
- Updating an existing key with
putcounts as a use and never evicts.