Design a surge pricing validator for supply and demand across geographic zones

Quick Overview

Design a class that tracks supply and demand events for geographic zones, computes a capped surge multiplier for each zone, and checks whether an order price falls inside the allowed range. It tests per-zone state design, the order of zero-supply and zero-demand edge cases, and careful floating-point price comparison.

Design a surge pricing validator for supply and demand across geographic zones

Company: Instacart

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a surge pricing validator that consumes a stream of supply and demand events for geographic zones and validates whether a given order price is within the allowed surge multiplier range. Each geographic zone has a base price. Supply and demand events affect the surge multiplier for a zone. The surge multiplier is calculated as `demand / supply` for that zone. If supply is `0` and demand is positive, the multiplier is capped at the configured maximum multiplier. If demand is `0`, the multiplier is `1.0` (no surge). The minimum surge multiplier is always `1.0`. An order price is valid if it falls within the range `[base_price * 1.0, base_price * surge_multiplier]` (inclusive on both ends). The surge multiplier is capped at a configurable `max_multiplier` provided at construction time. Implement the following methods: - `SurgePricingValidator(max_multiplier: float)`: Create the validator with the cap on the surge multiplier. - `add_zone(zone_id: str, base_price: float)`: Register a new geographic zone with the given base price. Initializes supply and demand to 0. - `supply_event(zone_id: str, count: int)`: Record a supply event by adding `count` supply units to the specified zone. - `demand_event(zone_id: str, count: int)`: Record a demand event by adding `count` demand units to the specified zone. - `get_surge_multiplier(zone_id: str) -> float`: Return the current surge multiplier for the zone, rounded to 2 decimal places. Returns `-1.0` if the zone does not exist. - `validate_price(zone_id: str, order_price: float) -> bool`: Return `True` if `order_price` is within `[base_price, base_price * surge_multiplier]`, `False` otherwise. Returns `False` if the zone does not exist. - `reset_zone(zone_id: str)`: Reset the supply and demand counters for the specified zone to zero. Example 1, with `max_multiplier = 3.0`: ```text add_zone("downtown", 10.0) get_surge_multiplier("downtown") -> 1.0 no demand yet supply_event("downtown", 4) demand_event("downtown", 6) get_surge_multiplier("downtown") -> 1.5 6 / 4 validate_price("downtown", 12.0) -> True 10.0 <= 12.0 <= 15.0 validate_price("downtown", 15.5) -> False above 10.0 * 1.5 validate_price("downtown", 9.99) -> False below the base price supply_event("downtown", 2) demand_event("downtown", 1) get_surge_multiplier("downtown") -> 1.17 7 / 6 = 1.1667, rounded ``` Example 2, with `max_multiplier = 2.0`: ```text add_zone("airport", 20.0) demand_event("airport", 5) get_surge_multiplier("airport") -> 2.0 supply is 0, so the cap applies supply_event("airport", 1) get_surge_multiplier("airport") -> 2.0 5 / 1 = 5.0, capped at 2.0 supply_event("airport", 9) get_surge_multiplier("airport") -> 1.0 5 / 10 = 0.5, raised to the 1.0 floor validate_price("airport", 20.0) -> True the range is [20.0, 20.0] reset_zone("airport") get_surge_multiplier("airport") -> 1.0 counters are back to zero get_surge_multiplier("unknown") -> -1.0 validate_price("unknown", 5.0) -> False ``` ```hint Order of the special cases Write down what a freshly added zone looks like (supply 0, demand 0) and decide which branch it should hit before you code the zero checks. ``` ```hint The price comparison Decide which multiplier value `validate_price` should compare against, and think about an order priced exactly at the upper bound when that bound comes from a floating-point multiplication. ``` ### Constraints & Assumptions - `1.0 <= max_multiplier <= 10.0` - Base prices are positive floats. - Supply and demand counts are non-negative integers. - Zone IDs are non-empty strings. - Order prices are non-negative floats. - At most 10,000 operations in total. ### Clarifying Questions to Ask - Should supply, demand and reset calls for a zone that was never added be ignored, or should they raise an error? - Does calling `add_zone` on an existing zone reset its counters, or only update the base price? - Should `validate_price` use the rounded multiplier that `get_surge_multiplier` reports, or the exact ratio? - Which rounding rule applies at an exact half, such as a ratio of 1.125? - Are supply and demand cumulative until a reset, or should old events expire after some time window? ### What a Strong Answer Covers - A per-zone record (base price, supply count, demand count) kept in a hash map, with constant-time operations. - The multiplier rules applied in an order that handles zero demand, zero supply, the 1.0 floor and the cap without dividing by zero. - A validation rule with inclusive bounds that agrees with the multiplier the getter reports. - Defined behavior for unknown zones on every method and for re-adding an existing zone. - Awareness of floating-point issues at the price boundary and in rounding, with a way to avoid them. ### Follow-up Questions - Supply and demand should only count events from the last five minutes. How would you change the data model? - Events arrive from many threads or consumers at once. How do you keep each zone's counters and multiplier consistent? - With small counts the multiplier jumps on every event. How would you smooth it so prices do not swing back and forth?

Overview: Design a class that tracks supply and demand events for geographic zones, computes a capped surge multiplier for each zone, and checks whether an order price falls inside the allowed range. It tests per-zone state design, the order of zero-supply and zero-demand edge cases, and careful floating-point price comparison.

Instacart logo
Instacart
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a surge pricing validator that consumes a stream of supply and demand events for geographic zones and validates whether a given order price is within the allowed surge multiplier range.

Each geographic zone has a base price. Supply and demand events affect the surge multiplier for a zone. The surge multiplier is calculated as demand / supply for that zone. If supply is 0 and demand is positive, the multiplier is capped at the configured maximum multiplier. If demand is 0, the multiplier is 1.0 (no surge). The minimum surge multiplier is always 1.0.

An order price is valid if it falls within the range [base_price * 1.0, base_price * surge_multiplier] (inclusive on both ends). The surge multiplier is capped at a configurable max_multiplier provided at construction time.

Implement the following methods:

  • SurgePricingValidator(max_multiplier: float) : Create the validator with the cap on the surge multiplier.
  • add_zone(zone_id: str, base_price: float) : Register a new geographic zone with the given base price. Initializes supply and demand to 0.
  • supply_event(zone_id: str, count: int) : Record a supply event by adding count supply units to the specified zone.
  • demand_event(zone_id: str, count: int) : Record a demand event by adding count demand units to the specified zone.
  • get_surge_multiplier(zone_id: str) -> float : Return the current surge multiplier for the zone, rounded to 2 decimal places. Returns -1.0 if the zone does not exist.
  • validate_price(zone_id: str, order_price: float) -> bool : Return True if order_price is within [base_price, base_price * surge_multiplier] , False otherwise. Returns False if the zone does not exist.
  • reset_zone(zone_id: str) : Reset the supply and demand counters for the specified zone to zero.

Example 1, with max_multiplier = 3.0:

add_zone("downtown", 10.0)
get_surge_multiplier("downtown")   -> 1.0     no demand yet
supply_event("downtown", 4)
demand_event("downtown", 6)
get_surge_multiplier("downtown")   -> 1.5     6 / 4
validate_price("downtown", 12.0)   -> True    10.0 <= 12.0 <= 15.0
validate_price("downtown", 15.5)   -> False   above 10.0 * 1.5
validate_price("downtown", 9.99)   -> False   below the base price
supply_event("downtown", 2)
demand_event("downtown", 1)
get_surge_multiplier("downtown")   -> 1.17    7 / 6 = 1.1667, rounded

Example 2, with max_multiplier = 2.0:

add_zone("airport", 20.0)
demand_event("airport", 5)
get_surge_multiplier("airport")    -> 2.0     supply is 0, so the cap applies
supply_event("airport", 1)
get_surge_multiplier("airport")    -> 2.0     5 / 1 = 5.0, capped at 2.0
supply_event("airport", 9)
get_surge_multiplier("airport")    -> 1.0     5 / 10 = 0.5, raised to the 1.0 floor
validate_price("airport", 20.0)    -> True    the range is [20.0, 20.0]
reset_zone("airport")
get_surge_multiplier("airport")    -> 1.0     counters are back to zero
get_surge_multiplier("unknown")    -> -1.0
validate_price("unknown", 5.0)     -> False

Constraints & Assumptions

  • 1.0 <= max_multiplier <= 10.0
  • Base prices are positive floats.
  • Supply and demand counts are non-negative integers.
  • Zone IDs are non-empty strings.
  • Order prices are non-negative floats.
  • At most 10,000 operations in total.

Clarifying Questions to Ask Guidance

  • Should supply, demand and reset calls for a zone that was never added be ignored, or should they raise an error?
  • Does calling add_zone on an existing zone reset its counters, or only update the base price?
  • Should validate_price use the rounded multiplier that get_surge_multiplier reports, or the exact ratio?
  • Which rounding rule applies at an exact half, such as a ratio of 1.125?
  • Are supply and demand cumulative until a reset, or should old events expire after some time window?

What a Strong Answer Covers Guidance

  • A per-zone record (base price, supply count, demand count) kept in a hash map, with constant-time operations.
  • The multiplier rules applied in an order that handles zero demand, zero supply, the 1.0 floor and the cap without dividing by zero.
  • A validation rule with inclusive bounds that agrees with the multiplier the getter reports.
  • Defined behavior for unknown zones on every method and for re-adding an existing zone.
  • Awareness of floating-point issues at the price boundary and in rounding, with a way to avoid them.

Follow-up Questions Guidance

  • Supply and demand should only count events from the last five minutes. How would you change the data model?
  • Events arrive from many threads or consumers at once. How do you keep each zone's counters and multiplier consistent?
  • With small counts the multiplier jumps on every event. How would you smooth it so prices do not swing back and forth?