Logo

Coding preview

Implement Reader-Writer Lock

What this preview is

About this preview

Implement Reader-Writer Lock is a hard quant coding problem on concurrency in Cpp, asked at Quant.

Unlock full access to getcracked

Join to unlock this problem, detailed solutions, and our complete library of quant finance interview prep.

Implementing a reader-writer lock with writer priority

This is a hard concurrency problem that tests your ability to design and implement a synchronization primitive in C++. Reader-writer locks are common in systems where read operations vastly outnumber writes—allowing concurrent readers while ensuring exclusive write access—but the twist here is writer priority, which prevents reader starvation of pending writers.

The core challenge is managing state across multiple threads using only locks and condition variables, while respecting the ordering constraint: writers must take precedence when both readers and writers are waiting. A clean solution tracks the number of active readers and writers, the number of pending writers, and uses condition variables to signal state changes. The lock must coordinate acquisition and release so that readers can proceed concurrently, but block as soon as a writer arrives—and block new readers if a writer is waiting.

  • Mutex and condition-variable patterns for thread synchronization
  • Reader-writer lock semantics and writer-starvation prevention
  • Return-value contracts (return counts of active/remaining threads)
  • Edge cases: first reader, last reader, first writer, exclusivity guarantees