The core mechanic they wanted was a per-key in-flight marker plus a condition variable, not just slapping a global mutex around everything.
Start by clarifying requirements and constraints, then propose a design using a concurrent map with per-key locking (e.g., striped locks or a ConcurrentHashMap of futures) to ensure only one computation per key. Walk through the locking strategy, how waiting threads are signaled (e.g., via CompletableFuture or condition variables), and how exceptions are propagated and handled. Finally, discuss trade-offs and potential optimizations.
Pro tip: Mention that you would use a ConcurrentHashMap with computeIfAbsent to atomically check-and-insert a placeholder future, which elegantly handles the single-computation guarantee without explicit locks. Also, highlight the importance of cleaning up failed computations to avoid caching exceptions permanently.
Ask about expected concurrency level, cache size limits, eviction policies, and whether the compute function is idempotent. This shows you consider practical aspects before diving into design.
Propose a ConcurrentHashMap that maps keys to futures or locks. Use per-key locking (e.g., striped locks or computeIfAbsent) to ensure only one thread computes per key, while others wait on the future.
Describe how waiting threads block on the future (e.g., CompletableFuture.get()) and are signaled upon completion. Mention that this avoids busy-waiting and handles timeouts if needed.
Detail how exceptions from the compute function are propagated to all waiting threads and how the cache entry is removed or marked as failed to allow retries. Emphasize not caching exceptions permanently.
Talk about lock granularity, memory overhead, eviction policies (e.g., LRU), and potential deadlocks. Mention alternatives like using a single lock for simplicity vs. per-key locks for scalability.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.