Calculate Ride Service Economics and Peak Pricing
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests unit economics, capacity planning, and dynamic pricing calculations for a transportation service with fixed driver costs and variable demand.
You're analyzing the economics of a transportation service. The service operates with drivers who are paid a fixed daily wage and work a set number of hours per day. Each driver can complete a maximum number of rides per hour based on service time and geography.
Write a function that calculates key financial metrics and determines optimal peak pricing.
Inputs
Your function receives nine parameters:
daily_rides(int): Total number of rides completed per daybase_price(float): Price charged per ride in dollarsdriver_daily_pay(float): Fixed amount paid to each driver per daymax_rides_per_driver_per_hour(int): Maximum rides one driver can complete in one hourdriver_hours(int): Number of hours each driver works per dayfixed_daily_cost(float): Daily fixed operating costs (technology, overhead, etc.)non_peak_rides(int): Number of rides during off-peak hourspeak_rides(int): Number of rides during peak hoursnon_peak_price(float): Price per ride during off-peak hours
Note: daily_rides equals non_peak_rides + peak_rides in all test cases.
Output
Return a dictionary with exactly five keys (all float values):
"drivers": Minimum number of drivers needed to handledaily_rides(round up if fractional)"daily_revenue": Total revenue from all rides atbase_price"driver_cost": Total cost of paying all drivers"daily_profit": Revenue minus driver cost minus fixed cost"target_peak_price": Price per peak ride that would achieve the samedaily_profitif non-peak rides are priced atnon_peak_priceand the same number of drivers is employed
Important Rules
- Drivers are paid their full daily wage regardless of how many rides they complete
- The number of drivers must be sufficient to handle the total
daily_ridesgiven each driver's capacity - When calculating
target_peak_price, assume the same number of drivers is needed (based on totaldaily_rides) - If
peak_ridesis zero, settarget_peak_priceto 0.0 - Round up fractional driver counts (you can't hire 2.3 drivers)
Constraints
- 0 ≤
daily_rides≤ 10,000 - 0 <
base_price≤ 100 - 0 <
driver_daily_pay≤ 2,000 - 1 ≤
max_rides_per_driver_per_hour≤ 10 - 1 ≤
driver_hours≤ 24 - 0 ≤
fixed_daily_cost≤ 100,000 - 0 ≤
non_peak_rides,peak_rides≤ 10,000 - 0 <
non_peak_price≤ 100 daily_rides=non_peak_rides + peak_rides
Examples
Example 1
Input: calculate_ride_service_metrics(2400, 30, 700, 5, 8, 10000, 800, 1600, 30)
Output: {'drivers': 60, 'daily_revenue': 72000, 'driver_cost': 42000, 'daily_profit': 20000, 'target_peak_price': 30}
Example 2
Input: calculate_ride_service_metrics(100, 25, 500, 5, 8, 1000, 40, 60, 25)
Output: {'drivers': 3, 'daily_revenue': 2500, 'driver_cost': 1500, 'daily_profit': 0, 'target_peak_price': 25}
Example 3
Input: calculate_ride_service_metrics(0, 30, 700, 5, 8, 10000, 0, 0, 30)
Output: {'drivers': 0, 'daily_revenue': 0, 'driver_cost': 0, 'daily_profit': -10000, 'target_peak_price': 0}
Hints
Hint 1
Calculate each driver's total daily capacity by multiplying their hourly ride limit by their working hours
Hint 2
The number of drivers needed is determined by dividing total rides by capacity per driver, rounding up to ensure all rides can be served
Hint 3
To find the target peak price, set up an equation where total revenue (non-peak + peak) minus costs equals the original daily profit
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem tests your ability to build a unit economics model and solve for an unknown price given a target profit constraint.
The solution proceeds in five steps:
- Calculate driver capacity: Each driver can complete
max_rides_per_driver_per_hour × driver_hoursrides per day - Determine staffing needs: Divide total rides by per-driver capacity and round up (you need enough drivers to handle all rides)
- Compute base-case financials: Revenue is rides times price; costs include both driver wages and fixed overhead; profit is the difference
- Set up the pricing equation: To maintain the same profit with differential pricing, total revenue must remain constant (since costs haven't changed)
- Solve for peak price: Given non-peak revenue, find the peak price that hits the revenue target
The key insight is that driver cost is fixed per driver per day—drivers must be paid their full wage even if they have idle time. This creates the economic motivation for surge pricing: if demand is uneven but drivers work the full day, you need to recover the cost of idle capacity by charging more during busy periods.
Why this is correct: The target peak price calculation ensures that (non_peak_rides × non_peak_price) + (peak_rides × target_peak_price) = original_daily_revenue, which with unchanged costs yields unchanged profit.
Edge case: When peak_rides is zero, there's no valid peak price, so we return 0.0. When daily_rides is zero, no drivers are needed.
Time complexity: O(1) — all operations are arithmetic calculations
Space complexity: O(1) — only storing a fixed number of values
import math
from typing import Dict
def calculate_ride_service_metrics(daily_rides: int, base_price: float, driver_daily_pay: float, max_rides_per_driver_per_hour: int, driver_hours: int, fixed_daily_cost: float, non_peak_rides: int, peak_rides: int, non_peak_price: float) -> Dict[str, float]:
capacity_per_driver = max_rides_per_driver_per_hour * driver_hours
drivers = math.ceil(daily_rides / capacity_per_driver) if daily_rides else 0
daily_revenue = daily_rides * base_price
driver_cost = drivers * driver_daily_pay
daily_profit = daily_revenue - driver_cost - fixed_daily_cost
target_total_revenue = daily_profit + driver_cost + fixed_daily_cost
target_peak_price = ((target_total_revenue - non_peak_rides * non_peak_price) / peak_rides) if peak_rides else 0.0
return {
"drivers": drivers,
"daily_revenue": daily_revenue,
"driver_cost": driver_cost,
"daily_profit": daily_profit,
"target_peak_price": target_peak_price,
}