
Design and implement a thread-safe cache system that support...
Prompt
Design and implement a thread-safe cache system that supports both TTL expiration AND LFU eviction. Requirements: - get(key) / put(key, value, ttl_seconds) - When the cache is full, evict the least frequently used non-expired key first; break ties by evicting the least recently used among those with the same frequency - Expired keys must not rely solely on lazy detection during get() β there must be an active background cleanup mechanism, otherwise a key that's never accessed again would occupy memory forever - Both get() and put() must run in O(1) average time complexity (this means you cannot use a simple scan or sort to find the minimum frequency β explain the data structure design that makes this possible) - Concurrent reads/writes must be correct in a multithreaded environment. Explain your locking strategy, and justify why it is NOT simply wrapping the whole method in one coarse-grained lock (analyze how lock granularity affects concurrency performance) Please provide: 1. Complete, runnable code (any language β specify which) 2. Complexity analysis (average case + worst case) 3. At least 5 test cases, including at least 2 tricky edge cases (e.g., capacity = 1, TTL = 0, multiple threads putting the same key simultaneously, eviction order when multiple keys have the same access frequency) 4. Finally, proactively point out: what trade-offs or potential bugs remain unresolved in your solution