NetflixOA #3
Timed Cache with Sidecar Cleanup
估计中等评测数据准备中,可先阅读题面
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>GETis 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.GETshould print the current value orNULLif the key is missing or expired. Function Description Complete the functionrunTimedCachein the editor below.runTimedCachehas the following parameter:String[] operations: cache operations executed in order ReturnsString[]: the output lines produced by the batch.
Constraints
GETshould stay cheap even when the cache is large.- Each key has a per-item TTL.
- A cleanup help
Example 1
Input:
operations = ["SET a 1 5 0", "GET a 3", "GET a 6"]
Output:
["1", "NULL"]
Explanation:
Key a expires at time 5. It is still valid at time 3 and expired by time 6.
Example 2
Input:
operations = ["SET a 1 5 0", "SET b 2 2 0", "CLEANUP 3", "GET a 3", "GET b 3"]
Output:
["1", "NULL"]
Explanation:
By time 3, key b has expired and may be removed during cleanup. Key a is still alive.