Three hard problems the way a mobility or delivery interview frames them: riders matched before they leave, an earliest arrival when roads slow down, and a courier tour with rules. A 130-minute round with a plan.
The stretch round has hard problems. The stories are still about riders, couriers and roads, but the model behind each one is a classic that you must name and defend. The interviewer listens for four habits.
The stretch round has three hard problems. They ask for the most valuable riders to match before they leave, the earliest arrival when roads slow down, and a courier tour with pickups before drop-offs. Together they take more than two hours.
This is the third of three rounds in the mobility and delivery set. Do the core round first: the heading in the state and the reasons behind a greedy choice carry over.
Exercise · Predict the output: The mask of visited stops. A stop can be visited only when the stop it needs has been visited, and the depot (place 0) counts as visited. Stop 2 needs stop 3, stop 3 needs stop 1, and stop 1 needs nothing. The code tries the stops in the order 2, 1, 3, 2. What does it print: the number of visits that were allowed, then the mask of visited places?
before = [-1, -1, 3, 1]
seen = 1
legal = 0
for stop in [2, 1, 3, 2]:
need = before[stop]
if need == -1 or (seen >> need) & 1:
legal += 1
seen |= 1 << stop
print(legal, seen)Each problem names its move in the statement. Read the signal, then use the idea the Academy already taught you.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| each driver serves one rider, and riders leave at a deadline | sort riders by deadline, keep a heap of chosen fares, and evict the cheapest when the slots run out | Scheduling with heaps |
| the travel time depends on the minute you enter a road, and waiting is allowed | Dijkstra with a heap ordered by arrival time | Dijkstra's shortest paths |
| every stop once, back to the depot, at most 12 stops, some stops first | a table over the set of stops visited and the last stop | Dynamic programming from recursion |
What is new here is the frame, not the idea. The heap holds fares, because the question is which riders to keep, not who goes next. A road's cost is a function of the entry minute, and the rush hour ends one minute before its end value, so the boundary needs care. And a rule that stop s needs stop p first is one bit test on the set of visited stops.
The rider problem has a tempting greedy rule: serve the highest fares first. That rule is right, but only if each rider takes the right driver.
Exercise · Break it: The earliest free driver. Riders are taken from the highest fare down. A driver can serve a rider when the driver is free by the rider's deadline, and each driver serves one rider. This gives each rider the earliest free driver that fits. Find an input where it earns less than the best total.
def max_fares(driver_times, deadlines, fares):
order = sorted(range(len(fares)), key=lambda r: -fares[r])
used = [False] * len(driver_times)
total = 0
for r in order:
best = -1
for d in range(len(driver_times)):
if not used[d] and driver_times[d] <= deadlines[r]:
if best == -1 or driver_times[d] < driver_times[best]:
best = d
if best != -1:
used[best] = True
total += fares[r]
return totalGive each rider the latest driver who still fits: an early driver can serve anyone, so spending that driver first can strand a rider.
The arrival problem has a boundary that decides many cases. The rush hour applies when the courier enters at a minute from begin up to, but not including, end.
Exercise · Put it in order: Arrival along a fixed route. Arrange the lines into a function that follows a list of roads in order and returns the arrival minute. Road i takes rush[i] minutes when the courier enters it at a minute from begin[i] up to, but not including, end[i], and normal[i] minutes otherwise.
def arrival(start, normal, rush, begin, end):
t = start
for i in range(len(normal)):
if begin[i] <= t < end[i]:
t += rush[i]
else:
t += normal[i]
return t
if begin[i] <= t <= end[i]:The rush hour runs from begin up to but not including end: entering at the end minute takes the normal time.
All three problems are hard, and together they make one stretch round. Each time box is close to what an interviewer allows for a hard problem when they want the model, the code and the bound.
Solve them in this order. The first adds a heap that evicts, the second changes the cost with time, and the third needs a table over sets.
Hard problems come with a follow-up that tests whether you understood the model. Prepare one for each problem.
Treat the three problems as one long session: 130 minutes in all, with a timer, and take a short break between problems. Work out loud and name the bound before you run.
If a problem takes longer than its box, finish it anyway, but note the overrun.
Checkpoint · Mobility and delivery stretch round
Q1/5 Drivers are free at minutes 0 and 5. Rider A pays 9 and leaves at minute 10. Rider B pays 8 and leaves at minute 3. Each driver serves one rider. What is the best total?
Answer: B
Rider B can only use the driver free at minute 0, and rider A can use the driver free at minute 5. Both are served, so the total is 17. Giving A the driver at minute 0 earns only 9.
Q2/5 A road takes 2 minutes normally and 5 minutes when entered from minute 10 up to, but not including, minute 20. The courier enters at minute 20. When does the courier arrive?
Answer: A
Minute 20 is the end of the rush hour and is not included, so the road takes 2 minutes. The courier arrives at minute 22.
Q3/5 A tour has 12 stops. How many different sets of stops can the table hold, counting the empty set?
Answer: B
Each stop is in or out of the set, so there are 2 to the power of 12 = 4,096 sets. With the last stop added, the table has 49,152 states.
Q4/5 When may the table add stop s to a set of visited stops, if stop p must come first?
Answer: A
The rule is met only when stop p has already been visited, which is one bit test on the mask. A set without p cannot take s.
Q5/5 A greedy rule gives the highest fare the earliest free driver. Why does it fail?
Answer: A
An early driver can serve any rider. Spending that driver on a rider who could wait leaves a rider with an early deadline with nobody to take them.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.