Surge pricing must reflect supply and demand from seconds ago, apply consistently to everyone in an area, and never change mid-request. What is the architecture?
Show the full answer Hide the answer
What the interviewer is testing
Whether you can reconcile "computed from streaming data" with "stable and consistent for a user interaction".
The tension
Continuous recomputation from a live stream gives freshness and produces a value that changes between the moment a user sees a price and the moment they accept it — which is commercially and legally unacceptable.
The design
Compute per spatial cell over a sliding window. Supply and demand events aggregate by cell over a short window, producing a multiplier per cell. The cell is the unit because pricing must be uniform for everyone in an area, and a cell id makes that explicit and queryable.
Publish the multiplier as a versioned value, not a continuously varying one. The pricing service holds the current published value per cell, updated on an interval rather than per event. This gives a stable, consistent answer to every concurrent request in that cell.
Quote and hold. When a user requests a price, the quote is captured with its multiplier version and held for a defined period. The user's price does not change during their decision, and expiry is explicit and communicated.
Smooth and bound the changes. Raw window output is jumpy — a handful of events can swing a sparse cell's ratio dramatically. Rate-limiting how fast a multiplier may move, and capping it, is both a product requirement and a defence against manipulation.
The event-time requirement
Aggregation must use event time, not processing time. A consumer outage that drains four hours of backlog in ten minutes would, under processing time, register as an enormous demand spike that never occurred — and price accordingly. That is a real failure mode with direct commercial consequence.
What a strong answer adds
The audit obligation: pricing decisions are frequently subject to regulatory scrutiny, so the inputs, the multiplier version and the quote must be reconstructable for any historical transaction. That means the computation and its inputs are retained and versioned, not merely the output.
And the sparse-cell problem: cells with few events produce statistically meaningless ratios, so the design needs a minimum-volume threshold with fallback to a coarser cell.
Common weak answers
Recomputing the price per request from the live stream. Using processing time for the aggregation.