Practice Free
HardBusiness Analytics High Frequency

Real-Time Supply-Demand Imbalance Surge Multiplier

Frequently Asked By:AppleGoogleAmazon
Interactive In-Browser Workspace

Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.

Solve in Workbench

Problem Statement

For each geographic zone in `zone_supply_demand`, compute `demand_supply_ratio` (`rider_requests::numeric / available_drivers`) and assign a surge multiplier: - If `demand_supply_ratio >= 3.0` THEN `2.5` - Else if `demand_supply_ratio >= 2.0` THEN `1.8` - Else if `demand_supply_ratio >= 1.5` THEN `1.3` - Else `1.0` Return: 1. `zone_id` 2. `zone_name` 3. `rider_requests` 4. `available_drivers` 5. `demand_supply_ratio`: `ROUND(rider_requests::numeric / available_drivers, 2)` 6. `surge_multiplier` Order results by `surge_multiplier` descending, then by `zone_id` ascending.

Why Technical Interviewers Ask This

Ride hailing pricing algorithms compute real-time surge multipliers per geographic zone based on the ratio of active rider demand requests to available nearby drivers.

Common Pitfalls & Interview Traps

Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass business analytics constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        zone_id,
        zone_name,
        rider_requests,
        available_drivers,
        ROUND(rider_requests::numeric / available_drivers, 2) AS demand_supply_ratio,
        CASE 
          WHEN (rider_requests::numeric / available_drivers) >= 3.0 THEN 2.5
          WHEN (rider_requests::numeric / available_drivers) >= 2.0 THEN 1.8
          WHEN (rider_requests::numeric / available_drivers) >= 1.5 THEN 1.3
          ELSE 1.0
        END AS surge_multiplier
      FROM zone_supply_demand
      ORDER BY surge_multiplier DESC, zone_id ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Business Analytics. It meets the target criteria: Geographic dispatch zones with calculated surge pricing multipliers.

Edge Cases to Verbally Clarify with Your Interviewer

The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.

Frequently Asked Follow-Up Questions

What makes this question a favorite among Apple and Google interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable Business Analytics queries that run efficiently on multi-million row production databases without excessive memory usage.

How does the query optimizer execute this query?

Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.

More FAANG SQL Interview Questions

View all questions