TTL Cache Operations
Reported by candidates from Oracle's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The constraint that kills the naive answer in this Oracle OA, reported September 2026, is the line saying GET must not scan the whole cache. If you loop over every entry on each read, you're done. This is a design problem built on a hash map plus an expiry structure, and the batch of SET, GET and CLEANUP strings is just parsing wrapped around it. You're taking it in a day or two, so you want the shape of the solution before you open the editor. StealthCoder is the invisible safety net if you blank mid-assessment, but the core idea fits in four lines.
The problem
Implement a key-value cache where each entry has its own TTL. Support the following operations: SET <key> <value> <ttlSeconds> <now> GET <key> <now> CLEANUP <now> GET is the hot path, so it should not scan the whole cache to remove expired entries. A separate sidecar or background process may perform proactive cleanup. Return the output lines produced by the operation batch. GET should print the current value or NULL if the key is missing or expired. Function runTimedCache(operations: String[]) → String[] Complete the function runTimedCache in the editor below. runTimedCache has the following parameter: String[] operations: cache operations executed in order Returns String[]: the output lines produced by the batch. Examples Example 1 operations = ["SET a 1 5 0", "GET a 3", "GET a 6"] return = ["1", "NULL"] Key a expires at time 5. It is still valid at time 3 and expired by time 6. Example 2 operations = ["SET a 1 5 0", "SET b 2 2 0", "CLEANUP 3", "GET a 3", "GET b 3"] return = ["1", "NULL"] By time 3, key b has expired and may be removed during cleanup. Key a is still alive. Constraints GET should stay cheap even when the cache is large. Each key has a per-item TTL. A cleanup helper may delete expired entries outside the hot path.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Store each key in a hash map as value plus expiry time, where expiry equals now plus ttl. GET is a single lookup: if the key is missing or expiry is at or below now, print NULL, otherwise print the value. Check the boundary against the examples. Expiry 5 is valid at 3 and gone at 6, but time exactly 5 is your call, so state it and stay consistent. For CLEANUP, push (expiry, key) into a min-heap on every SET and pop while the top expiry is at or below now. The pitfall is stale heap entries. When a key gets overwritten with a new TTL, the old heap entry still points at it. Before deleting, confirm the map's current expiry matches the popped one. Parse tokens by splitting on spaces and collect only GET outputs. If the parsing or heap logic slips under pressure, StealthCoder can cover you live.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill TTL Cache Operations 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Oracle's OA.
Oracle reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
TTL Cache Operations FAQ
How hard is the TTL Cache question really?+
Easy to medium. The data structures are simple, a hash map and maybe a heap. The difficulty is handling details: overwritten keys, the expiry boundary, and parsing the operation strings. Most of the points come from getting those edge cases right, not from clever algorithms.
What's the trick to keeping GET cheap?+
Make GET a single hash map lookup that compares the stored expiry to the current time. Expired entries get treated as missing right there, so GET never scans anything. Physical deletion is deferred to CLEANUP, which is allowed to do the heavier work.
Do I even need a heap for CLEANUP?+
Not strictly. A full scan in CLEANUP would pass the examples since only GET must stay cheap. But a min-heap keyed on expiry is cleaner and faster, and it shows you understood the sidecar hint. Use it if you have time, scan if you're stuck.
What edge cases should I test before submitting?+
Overwriting a key with a new TTL, GET on a key that never existed, and a GET at exactly the expiry time. Also run CLEANUP on an empty cache. Confirm your heap ignores stale entries from overwritten keys, or you'll delete live data.
How do I prepare for this in 48 hours?+
Write a small cache with a map and a min-heap from scratch, twice. Practice parsing space-separated commands into a switch. Then do one LRU-style design problem to warm up on class-style thinking. That covers nearly everything this OA touches.