← Coinbase Interview InsightsSoftware Engineer
Spent over 50 minutes on this and still had 4 test cases failing at the end.
Start by clarifying the requirements and constraints, then propose a design using a hash map for key-value storage and a separate lock manager that tracks which user holds the lock on each key. Implement the operations with proper synchronization to ensure thread safety, and discuss trade-offs such as lock granularity and potential deadlocks.
Pro tip: Mention that locks should be automatically released when the user session ends or after a timeout to prevent indefinite blocking, and consider using a read-write lock per key to allow concurrent reads while ensuring exclusive writes.
Ask about concurrency expectations, persistence, lock timeout, and whether locks are exclusive to a single user or can be shared. Confirm the exact semantics of setByUser, deleteByUser, lock, and unlock.
Propose a hash map for key-value storage and a separate map for locks, where each key maps to the user ID holding the lock. Consider using a concurrent hash map or adding synchronization for thread safety.
For setByUser and deleteByUser, check if the key is locked by another user; if so, reject the operation. For lock, atomically set the lock if not already held; for unlock, verify the user and release the lock.
Use fine-grained locking (e.g., per-key locks) to avoid bottlenecks. Discuss handling of lock timeouts, reentrancy, and potential deadlocks. Ensure atomicity of check-and-set operations.
Talk about trade-offs between simplicity and scalability, and mention possible extensions like distributed locking, persistence, or lock expiration.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.