Implement a Writer-Preferring Read-Write Lock

Last updated: November 29, 2026

Quick Overview

Implement a read-write lock that allows many concurrent readers or one writer, and prevents writers from starving when reads are continuous.

Google
Software Engineering Fundamentals
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

0

0

0 solved


Implement a read-write lock that allows many concurrent readers or one writer, and prevents writers from starving when reads are continuous.

Read-mostly data such as routing tables or feature configs are common in Google servers, so interviewers ask candidates to build the primitive and reason about fairness. They check invariants, starvation, and the trade-offs of reader versus writer preference.

What the Interviewer Expects
  • States the invariants: readers > 0 implies no writer
  • Explains writer starvation with reader preference
  • Blocks new readers while a writer waits
  • Releases correctly and wakes the right threads
  • Discusses re-entrancy and upgrade pitfalls
Key Topics to Cover
read-write lock
starvation
condition variables
fairness
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • Can a reader upgrade to a writer? What goes wrong?
  • How would you make this fair in arrival order?
  • When is a plain mutex faster than a read-write lock?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • acquire_read/release_read and acquire_write/release_write.
  • Many readers concurrently; a writer is exclusive.
  • If a writer is waiting, new readers must wait so a constant stream of reads c...
High-Level Design

State under one condition variable: active reader count, a writer flag, and a count of waiting writers. Readers enter only when there is no active writer and nobody waiting to write. Writers register ...


Submit Your Answer
Markdown supported