Three easy problems the way a mobility or delivery interview frames them: customers counted by zone and hour, delivery slots that clash, and the nearest free drivers. A 50-minute round with a plan.
A mobility or delivery company moves people and parcels across a city: rides, couriers, scooters and orders. Its coding interviews use the same data structures as any other company, but the stories have a map, a clock and a crowd. The interviewer listens for four habits.
The warm-up round tests these habits on three easy problems. The interviewer often opens with "What would you ask before writing code?" Have your questions ready: how distance is measured, what breaks a tie, whether a slot end is included and what counts as one customer.
This is the first of three rounds in the mobility and delivery set. The core and stretch rounds that follow use the same habits on harder problems. They cover routes with few turns, dispatch under deadlines and travel times that change at rush hour.
Exercise · Predict the output: Customers, not orders. Five orders are logged with a zone, a customer and a time in seconds. The hour of an order is its time divided by 3,600, rounded down. How many different customers ordered in zone 3 during hour 1?
zones = [3, 3, 3, 3, 5]
customers = [7, 7, 8, 9, 7]
times = [3700, 3900, 5000, 7300, 3800]
seen = {}
for z, c, t in zip(zones, customers, times):
seen.setdefault((z, t // 3600), set()).add(c)
print(len(seen[(3, 1)]))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 |
|---|---|---|
| different customers for each zone and hour | a map from a zone-and-hour key to a set of customers | Counting and grouping |
| time slots that may overlap, with a travel buffer | sort by start and keep the latest end so far | Intervals |
| the k closest free drivers, ties by number | a heap that keeps the best k seen so far | Heaps and priority queues |
What is new here is the frame, not the idea. The key has two fields, and the hour is the time divided by 3,600 and rounded down, so hour 0 holds seconds 0 to 3,599.
A slot that clashes still counts as an earlier slot for the ones after it. So the latest end is a maximum, never the end of the previous slot. A busy driver must never reach the heap.
The slots problem is where a small slip hides. A long slot can contain a short one, and the next slot may still start before the long one ends.
Exercise · Find the bug: The end of the previous slot. Slots are sorted by start. This counts the slots that start before an earlier slot has ended plus the buffer. It is right when each slot ends after the one before it, but wrong when a long slot contains a short one. Which line is wrong?
def count_clashes(starts, ends, buffer):
latest = ends[0]
clashes = 0
for i in range(1, len(starts)):
if starts[i] < latest + buffer:
clashes += 1
latest = ends[i]
return clashesKeep the largest end seen so far: a short slot inside a long one must not hide the long one.
The nearest-driver problem ends with a filter that is easy to forget. The pickup point here is the map origin.
Exercise · Predict the output: Only the free drivers. The pickup point is the origin, and distance counts blocks east-west plus blocks north-south. Driver 1 is busy. The two closest free drivers are offered the ride, and ties go to the lower number. Which driver numbers are printed?
xs = [1, 4, 2, 5]
ys = [1, 0, 3, 5]
idle = [1, 0, 1, 1]
k = 2
candidates = [(abs(xs[i]) + abs(ys[i]), i) for i in range(4) if idle[i] == 1]
candidates.sort()
print(*[i for d, i in candidates[:k]])Filter to the free drivers before the heap: a busy driver in the best k takes a place a free driver should have.
All three problems are easy, and together they make one warm-up round. Each time box is close to what an interviewer allows for an easy problem, with time to explain.
Solve them in this order. The first is one pass with a map of sets, the second sorts and keeps one number, and the third adds a heap.
A warm-up problem rarely ends at the first answer. Once your code works, the interviewer changes one rule and asks what changes in your code. Prepare one follow-up for each problem.
Treat the three problems as one interview: 50 minutes in all, with a timer. Work out loud, as if someone is listening.
If a problem takes longer than its box, finish it anyway, but note the overrun. In a real round the interviewer would have moved on, so the next attempt should be faster.
Checkpoint · Mobility and delivery warm-up round
Q1/5 Two free drivers are the same distance from the pickup. What should the code do?
Answer: B
The result must be the same on every run, so the tiebreak in the statement decides. A heap order that changes with the input order is not a rule.
Q2/5 A zone has three orders in one hour, from customers 7, 7 and 8. What does the query answer?
Answer: B
The question counts different customers, not orders. Customers 7 and 8 are two customers, so the answer is 2.
Q3/5 Slots start at minutes 0, 1 and 5 and end at 10, 3 and 6, with no buffer. How many slots start before an earlier slot has ended?
Answer: B
Slots 2 and 3 both start before minute 10, when the first slot ends. Using the end of the previous slot finds only 1.
Q4/5 An order is placed at second 3,600. Which hour holds it?
Answer: B
Hour 0 holds seconds 0 to 3,599, so second 3,600 is the first second of hour 1. The hour is the time divided by 3,600, rounded down.
Q5/5 Drivers 0 to 3 are at distances 2, 4, 5 and 10, and driver 1 is busy. Which two drivers are offered a ride?
Answer: B
Driver 1 is busy, so the free drivers are 0, 2 and 3 in distance order. The first two are drivers 0 and 2.
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.