KatInterview

Design a Dispatch Queue for Autonomous Taxi Requests

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designheaphash-tabledesign

Last updated

Simulate an event-driven dispatching system that maintains pending requests ordered by pickup time and processes state updates efficiently using heaps and hash maps.

You are asked to build the core dispatch engine for an automated transport fleet. The system handles ride booking requests, cancellations, periodic state progression based on time, and querying current pending requests.

Implement a function ride_scheduler(operations: List[str], arguments: List[List]) -> List that receives a sequence of command names alongside their respective argument lists and executes them in sequential order. Return a list containing the output of each command in the order they were executed.

The supported operations are:

  1. add_ride(ride_id: str, pickup_time: int, location: str) Registers a request identified by ride_id with a scheduled pickup_time (seconds) and location string. Overwrites any existing request matching ride_id. Returns None.
  2. cancel_ride(ride_id: str) Cancels the active request with the specified ride_id. If ride_id is unknown or the ride has already started, no action is taken. Returns None.
  3. process_events(current_time: int) Advances system state up to timestamp current_time. All pending rides scheduled at or before current_time (pickup_time <= current_time) transition to started and are no longer pending. Returns None.
  4. get_pending_rides() Returns a list of ride_id strings for all currently active, non-started requests. The list must be ordered primarily by pickup_time in ascending order. When timestamps tie, break ties lexicographically by ride_id.

Constraints

  • $1 \le \text{len(operations)} \le 10^4$
  • pickup_time and current_time are non-negative integers ($\le 10^9$).
  • ride_id and location are non-empty strings.
  • Timestamps passed into process_events may not be strictly increasing. If current_time is less than a previously processed timestamp, no new rides are processed.

Examples

Example 1

Input: ride_scheduler(['add_ride', 'add_ride', 'get_pending_rides', 'process_events', 'get_pending_rides'], [['ride_1', 100, 'Point A'], ['ride_2', 50, 'Point B'], [], [75], []])

Output: [None, None, ['ride_2', 'ride_1'], None, ['ride_1']]

Example 2

Input: ride_scheduler(['add_ride', 'add_ride', 'get_pending_rides', 'process_events', 'get_pending_rides'], [['a', 10, 'X'], ['b', 10, 'Y'], [], [10], []])

Output: [None, None, ['a', 'b'], None, []]

Example 3

Input: ride_scheduler(['add_ride', 'add_ride', 'cancel_ride', 'get_pending_rides'], [['late', 20, 'A'], ['early', 5, 'B'], ['early'], []])

Output: [None, None, None, ['late']]

Hints

Hint 1

Use a combination of a min-heap for ordered time processing and a dictionary to maintain the ground truth of pending rides.

Hint 2

When a ride is canceled or updated with a new scheduled time, avoid searching the min-heap. Use lazy deletion instead by ignoring invalidated heap records during processing.

Hint 3

For `get_pending_rides()`, construct the list by sorting the active records from your ground-truth dictionary by primary key `pickup_time` and secondary key `ride_id`.