Implement an LRU Cache
Reported by candidates from Geico's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Geico asked this in May 2026, and it's a classic design question disguised as a data structures problem. You need to build an LRU cache that handles get and put in O(1) time per operation. The trick is pairing a hash map with a doubly linked list so you can evict the least recently used item instantly when capacity is exceeded. StealthCoder will catch you if you blank on the implementation details during the live OA.
The problem
Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem. Problem Implement an LRU (Least Recently Used) cache that supports: LRUCache(capacity): initialize the cache with the given positive capacity. get(key): return the value if the key exists and mark it as most recently used; otherwise return -1. put(key, value): insert or update the key with the value and mark it as most recently used; if the cache exceeds capacity, evict the least recently used entry. Requirements: Target time complexity: O(1) per get/put. Constraints Number of operations: 1 <= N <= 2 * 10^5 key, value are 32-bit integers Sample Tests Input: capacity=2 put(1,1) put(2,2) get(1) put(3,3) get(2) put(4,4) get(1) get(3) get(4) Output: 1 -1 3 4 Input: capacity=1 put(1,1) put(2,2) get(1) get(2) Output: -1 2 Input: capacity=2 put(1,1) put(1,10) get(1) Output: 10 Input: capacity=3 get(42) Output: -1 Input: capacity=2 put(1,1) put(2,2) put(3,3) get(1) get(2) get(3) Output: -1 2 3 Example Input capacity=2 put(1,1) put(2,2) get(1) put(3,3) get(2) put(4,4) get(1) get(3) get(4) Output 1 -1 3 4 Function Description Complete solveLRUCacheOperations. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters. The returned string array must match the expected standard output lines for the sample input. Use the limits and requirements stated in the prompt.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The core challenge is maintaining O(1) access and O(1) eviction. A hash map alone gets you fast lookups but not fast eviction. A linked list alone gets you order but not fast access. Combine them: hash map keys point to nodes in a doubly linked list. On get, move the node to the front (most recent). On put, either update the existing node or insert a new one at the front. When capacity is exceeded, delete the tail node (least recent). Most candidates overthink the node structure or forget to update pointers on all paths. StealthCoder bridges that gap if you're stuck on the exact sequence of pointer swaps.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Implement an LRU Cache cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as lru cache. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Geico's OA.
Geico reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Implement an LRU Cache FAQ
Do I really need a doubly linked list, or can I use a deque?+
A deque works if your language supports efficient removal from the middle (Python's deque does not). A doubly linked list is safer because you can remove any node in O(1) given a reference to it. That's what the hash map buys you: the reference.
What's the most common mistake on this problem?+
Forgetting to move the node to the front on get, or accidentally breaking pointer chains when removing nodes. Also, not handling the update case on put (when the key already exists). Test with put(1,1) then put(1,10) to catch this.
Does the order of operations matter if I'm evicting?+
Yes. Evict before inserting, not after. If you insert first and then check capacity, you'll have the wrong node as the victim. Insert, check capacity, evict tail if needed.
How do I handle the case where the same key is put twice?+
Update the value in the existing node and move that node to the front. Don't create a duplicate. The hash map key stays the same, the node reference doesn't change, only the value and position do.
Is there a language where this is significantly easier?+
Python's OrderedDict or collections.deque plus a dict will work, but you'll still need to manage moves manually. Java LinkedHashMap has automatic LRU semantics if you override removeEldestEntry. C++ unordered_map plus list is the standard approach.