Uber Surge Pricing Concurrent System

Hard
ConcurrencyRead-Write LockConcurrentHashMapAtomic OperationsPub-SubReal-Time

Problem Statement

Design the concurrent demand-monitoring and surge pricing system for a ride-sharing platform. The system tracks real-time ride requests and available drivers per geographic zone, computing a surge multiplier. Multiple threads concurrently update demand metrics and read surge prices.

class SurgePricingSystem {
    public SurgePricingSystem(double baseSurgeThreshold) {}
    // Called concurrently by request-handler threads
    public void recordRideRequest(String zoneId) {}
    // Called concurrently by driver-update threads
    public void recordDriverAvailability(String zoneId, int availableDrivers) {}
    // Called by pricing threads — must be fast (many reads per second)
    public double getSurgeMultiplier(String zoneId) {}
    // Background job: recalculate surge for all zones
    public void recalculateSurge() {}
    // Subscribe to surge changes
    public void onSurgeChange(String zoneId, Consumer<Double> listener) {}
}

Key Entities

  • Zone metrics: ConcurrentHashMap<String, ZoneMetrics> where ZoneMetrics holds request rate, driver count, and computed multiplier.
  • ReadWriteLock per zone (or StampedLock): Multiple readers (pricing) vs. exclusive writers (recalculation).
  • ScheduledExecutorService: Runs recalculateSurge() every N seconds.
  • AtomicDouble / volatile double surgeMultiplier: Frequently-read surge value per zone.

Requirements

  1. getSurgeMultiplier() must be extremely fast (microsecond latency) — it's on the critical path of every fare estimate.
  2. recordRideRequest() must be non-blocking — called from thousands of concurrent HTTP handler threads.
  3. Surge recalculation runs periodically in background without blocking the read path.
  4. Zone metrics updates (recordRideRequest, recordDriverAvailability) must be thread-safe.
  5. Surge change listeners are notified asynchronously when multiplier changes.

Asked at: Uber, Lyft, Ola, Grab, Amazon, Google

!Constraints

- Up to 10,000 zones worldwide. - getSurgeMultiplier() is called ~1 million times per second. - recordRideRequest() is called ~100,000 times per second. - Surge recalculation runs every 30 seconds. - Surge multiplier is a double between 1.0 and 10.0.
Output

Run or submit your code to see output here.