Implement a Read-Write Lock That Prevents Writer Starvation
Last updated: November 29, 2026
Quick Overview
Implement a reader-writer lock that allows many concurrent readers or a single writer, and guarantees that a writer waiting for the lock is not starved by a continuous stream of new readers.
Microsoft
November 29, 20260
0
0 solved
Implement a reader-writer lock that allows many concurrent readers or a single writer, and guarantees that a writer waiting for the lock is not starved by a continuous stream of new readers.
Read-mostly shared state such as configuration caches, routing tables, and in-memory indexes is common in Azure services and Windows components (Windows exposes SRW locks for exactly this). Microsoft interviewers use this to probe condition variable reasoning, fairness policies, and whether you can argue correctness rather than just pass a test.
What the Interviewer Expects
- States the safety invariant: writer exclusive, readers share, never both
- Explains why a naive reader-preferring lock starves writers
- Tracks waiting writers and blocks new readers while any are waiting
- Uses a condition variable with predicate loops and notify_all where needed
- Discusses the opposite risk of reader starvation and fair alternatives
- Mentions reentrancy, upgrades, and why upgrade can deadlock
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- With your policy, can readers starve? How would you make the lock fair?
- Why is upgrading a read lock to a write lock deadlock-prone, and how do some APIs handle it?
- When is a read-write lock slower than a plain mutex?
- How would you replace this with RCU or copy-on-write for a read-mostly routing table?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Operations: acquire_read, release_read, acquire_write, release_write. Safety: at any time either zero or more readers, or exactly one writer, never both. Liveness: a waiting writer eventually ...
High-Level Design
State under one mutex: active reader count, a writer-active flag, and a count of waiting writers. A reader may enter only if no writer is active and none is waiting; checking waiting writers is what p...