Katkat

Implement Food Delivery Billing

RipplingSoftware Engineer · Onsite

Last updated

This problem tests your ability to implement complex billing logic involving conditional fee structures, time-based surcharges, and data aggregation.

Problem Statement

You are building a billing service for a food delivery platform. You need to calculate the total delivery fee for a list of delivery tasks. Each task has a start time, end time, and a base distance.

Billing Rules:

  1. Base Fee: Every delivery has a base fee of $2.00.
  2. Distance Fee: $0.50 per kilometer.
  3. Time Surcharge:
    • If the delivery takes longer than 30 minutes, an additional "delay fee" of $5.00 is applied.
    • For deliveries occurring during "Peak Hours" (18:00 to 21:00 inclusive), an additional 20% surcharge is applied to the subtotal (Base + Distance + Delay).

Implementation:

Write a function calculate_payout(deliveries) that takes a list of delivery objects and returns the total payout for all deliveries, rounded to two decimal places.

Data Structure:

Each delivery is represented as a dictionary:

  • start_time: ISO format string (e.g., "2023-10-01T17:45:00")
  • end_time: ISO format string (e.g., "2023-10-01T18:15:00")
  • distance_km: float

Example:

deliveries = [
    {"start_time": "2023-10-01T18:00:00", "end_time": "2023-10-01T18:40:00", "distance_km": 2.0}
]
# Base: 2.0, Distance: 1.0 (2*0.5), Delay: 5.0 (40 mins > 30)
# Subtotal: 8.0. Peak surcharge (20%): 8.0 * 1.2 = 9.6
# Result: 9.6

Constraints:

  • Assume all times are on the same day.
  • Round all individual delivery subtotals to four decimal places before final summation to minimize floating-point errors.
  • The output should be a single float rounded to two decimal places.

Hints

Hint 1

Use a datetime library to parse the ISO strings and calculate the difference in minutes.

Hint 2

Determine the 'Peak Hour' status by checking if the start_time hour falls within the 18-21 range.

Hint 3

Handle the logic for individual costs sequentially before adding them to a running total for all deliveries.

Follow-up questions

  • How would you modify your solution if the peak hour surcharge applied only to the portion of the delivery time that overlapped with the 18:00-21:00 window?
  • If the number of deliveries grows to millions per day, how would you optimize the billing calculation? (e.g., batching, map-reduce)
  • How would you handle timezone differences if the platform expands to support international delivery centers?