As reported: Autodesk (2026), online assessment: design an in-memory database with TTL — a cache whose entries expire, like LeetCode 2622.
This follows LeetCode 2622 — Cache With Time Limit, with the current time passed in explicitly so it can run in any language.
Implement TimeLimitedCache:
set(now, key, value, duration)— storevalueunderkey, expiringdurationmilliseconds afternow. Returntrueifkeyalready had an unexpired value (it is overwritten, including its expiry), elsefalse.get(now, key)— return the value ifkeyexists and is unexpired, else-1.count(now)— return the number of unexpired keys.
A key set at time t with duration d is unexpired at times now < t + d and expired from t + d on.
Example
["TimeLimitedCache","set","get","count","get"]
[[],[0,1,42,100],[50,1],[50],[150,1]]
-> [null,false,42,1,-1]
At time 0, key 1 is stored for 100 ms. At 50 it is alive (42, count 1); at 150 it has expired.
Constraints: 0 ≤ key, value ≤ 10^9; 1 ≤ duration ≤ 10^9; 0 ≤ now ≤ 10^9, and now never decreases from one call to the next; at most 10^5 calls.
Code runner format: LeetCode's design format — line 1 the method names, line 2 their arguments. The harness prints the list of results (null for the constructor).