Fuel station queue with three dispensers
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Tesla. It tests simulation, queue and greedy.
A fuel station has three dispensers named X, Y and Z. Each starts with a known number of liters. A queue of cars waits at the station, and all of them are in line at time 0. Car i needs demands[i] liters.
Rules:
- Only the car at the front of the queue chooses a dispenser. Cars never overtake one another.
- The car may use a dispenser only if it is free (not serving a car) and holds at least the car's demand.
- If several dispensers qualify, the car takes the first by name:
X, thenY, thenZ. - If none qualifies right now, the car waits at the front and blocks everyone behind it.
- Fuel flows at 1 liter per second, so a car with demand
dkeeps its dispenser busy fordseconds. A dispenser that finishes at timetcan serve the next car at timet.
A car's waiting time is the time it starts refuelling. Implement:
def max_wait(demands: list[int], x: int, y: int, z: int) -> int
Return the longest waiting time over all cars. Return -1 if some car can never refuel.
Constraints: 0 <= len(demands) <= 10^5; 0 <= demands[i], x, y, z <= 10^9.
Example 1: demands = [3, 5, 4, 2], x = 10, y = 5, z = 1 returns 7.
- At 0, car 1 takes
X(7 liters left, busy until 3). Car 2 takesY(0 left, busy until 5). - Car 3 needs 4 liters. Only
Xhas enough, so it waits and starts at 3 (3 left, busy until 7). - Car 4 needs 2 liters. Only
Xhas enough, so it starts at 7.
Example 2: demands = [5, 1], x = 5, y = 0, z = 0 returns -1. Car 1 empties X, and no dispenser has fuel for car 2.
Clarifying questions
- Can a car with demand 0 use any free dispenser?
- When the front car waits, should it take the first dispenser by name among those free at the moment it can start?
- Is 'never' decided once no dispenser's remaining fuel can cover the front car (fuel only goes down)?
What a strong answer covers
- Simulates the queue in order, keeping each dispenser's remaining fuel and the time it becomes free, and the current time.
- For the front car: if no dispenser has enough fuel left, returns -1 (fuel never rises); otherwise moves time to the earliest moment one with enough fuel is free, then takes the first by name among those free then.
- Never lets a later car start earlier than the car before it, because cars do not overtake.
- Runs in O(n): constant work per car with three dispensers.
- Tests both examples, demand 0, a car waiting on the only dispenser with fuel, and an empty queue (0).
Follow-ups
- Any number of dispensers: keep them in a heap by free time.
- Cars arrive over time instead of all at time 0.
- Cars may skip ahead to a free dispenser, and how that changes fairness.
Where it's asked
What it tests
simulation · queue · greedy
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
