Spent over 50 minutes on this and still had 4 test cases failing at the end.
Clarify the requirements and constraints first, then design a data structure that combines a hash map for key-value storage with per-key metadata for locking. Implement the operations with proper synchronization to handle concurrent access, and discuss trade-offs around consistency, scalability, and failure handling.
Pro tip: Emphasize that locking is per-key and user-specific, and discuss how to handle edge cases like lock expiration and reentrancy. Also, mention that in a distributed setting, you'd need a consensus protocol or a centralized lock service to avoid race conditions.
Ask about expected scale, concurrency, persistence, and whether the store is in-memory or distributed. Confirm that only the locking user can modify the key, and whether locks expire or can be forcibly released.
Propose a hash map for key-value storage and a separate map for lock metadata (e.g., key -> {userId, timestamp}). Consider using a concurrent hash map or adding synchronization for thread safety.
Define setByUser and deleteByUser to check lock ownership before modifying. Implement lock to set the lock if not already locked, and unlock to clear it only if the requester is the owner.
Use locks or atomic operations to prevent race conditions. Discuss handling of lock timeouts, reentrant locks, and error responses for unauthorized access.
Compare in-memory vs. distributed implementations, and mention consistency models (e.g., strong vs. eventual). Highlight potential bottlenecks and solutions like sharding or using Redis with Lua scripts.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.