Prevent Double Assignment of a Driver Under Concurrent Dispatch

Last updated: November 29, 2026

Quick Overview

Several dispatch workers may try to give the same driver a trip at the same moment. Implement a thread-safe assigner where exactly one trip wins a free driver, retries are idempotent, and stale releases cannot free a driver from a newer trip.

Uber
Software Engineering Fundamentals
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


Several dispatch workers may try to give the same driver a trip at the same moment. Implement a thread-safe assigner where exactly one trip wins a free driver, retries are idempotent, and stale releases cannot free a driver from a newer trip.

Uber's matching runs many workers in parallel, so check-then-act races on driver state are a real bug class. The interviewer checks that you spot the race, choose a lock granularity that does not serialize the whole fleet, and think about idempotent retries and release semantics.

What the Interviewer Expects
  • Identify the check-then-act race on driver availability
  • Make check and set a single atomic step
  • Use striped or per-key locking instead of one global lock
  • Make repeated assigns of the same trip idempotent
  • Guard release so only the current holder can free the driver
Key Topics to Cover
Race conditions
Lock striping
Atomic check-and-set
Idempotency
Thread safety
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
  • How does this change when the assigner runs on many machines instead of threads?
  • What would a compare-and-set in Redis or a SQL conditional update look like?
  • How do you avoid deadlock if one operation needs two drivers' locks?
  • How would you test that the race is really gone?
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

try_assign(driver, trip) returns True for exactly one trip while the driver is free; calling it again with the same trip returns True (retry safe). release(driver, trip) frees the driver only if t...

High-Level Design

The bug in naive code is if driver not in busy: busy[driver] = trip: two threads both see the driver free and both assign. The fix is to make the check and the write one critical section per driver....


Submit Your Answer
Markdown supported