44. Design Ride-Hailing with Surge Pricing (Uber / Rapido)
1
The interview prompt2
44.1 What do you ask first?3
44.2 How big is this, really?4
44.3 What are the doors?5
44.4 What do we store?6
44.5 What does the whole system look like?7
44.6 How are location updates handled?8
44.7 How does matching work?9
44.8 How does surge pricing work?10
44.9 How does the trip state machine work?11
44.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab44. Design Ride-Hailing with Surge Pricing (Uber / Rapido)
44.8 How does surge pricing work?
3 min read+10 XP
When a concert ends, 300 people request rides in a cell with 100 drivers. Surge raises the price there to reduce demand (some people wait or walk) and attract supply (drivers head to the area):
Figure: surge nudges supply and demand together; the trip moves through fixed states.
- Per cell, per minute: count open requests and available drivers in each cell (from the request stream and the driver index).
- Multiplier from the ratio: 1.0 when supply covers demand, rising with the shortage, capped (say ×2.5).
- Smooth it: move part-way toward the target each minute, so prices don't jump around. In the chapter's code: ×1.5, then ×1.75, a spike to ×2.12 (under the ×2.5 cap), then easing to ×1.38 when demand drops.
- Lock it into the quote: the rider pays the multiplier they saw, even if surge changes during the trip.
- Guardrails: caps, and policies to limit surge during emergencies; show the multiplier clearly before booking.
Memory code: "Price the shortage, smooth the jumps, cap the top, lock the quote."