Three medium problems the way a mobility or delivery interview frames them: surge minutes counted once, courier trips batched by deadline, and a route with the fewest turns. An 85-minute round with a plan.
The warm-up round of this set checked a defined distance, a fixed tiebreak, slots and one pass over a log. The core round keeps those habits and adds medium problems, where the cost of a move depends on more than where you are. The interviewer listens for four new habits.
The core round has three medium problems: a surge that must count each minute once, couriers that batch orders by deadline, and a route with the fewest turns. They take about an hour and a half in all.
This is the second of three rounds in the mobility and delivery set. Warm up first with the warm-up round if distance, ties and slots still feel slow.
Exercise · Predict the output: Turns, not cells. A route is a string of moves, and the first move is free. Every change of direction is one turn. The app compares a short route with a longer one. What does this print for the two routes?
routes = ["RURU", "RRRRUU"]
for r in routes:
turns = 0
for i in range(1, len(r)):
if r[i] != r[i - 1]:
turns += 1
print(turns, end=" ")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 |
|---|---|---|
| k consecutive minutes with a total at least a threshold | a running window total, and a mark for the first minute not yet counted | Fixed and variable windows |
| trips of limited size, orders with a ready time and a last time | leave as late as the most urgent order allows, and take the orders that expire first | Greedy choices |
| fewest turns, not fewest cells | a search over states of cell and heading, with straight moves free | BFS and DFS on a grid |
What is new here is the frame, not the idea. A window ending at minute i covers k minutes, and the next qualifying window overlaps it, so the code must add only the minutes it has not counted. The trip rule needs a reason, because a greedy rule without one is a guess. And the route has four headings for every cell, so it has four times as many states as the grid has cells.
The surge problem is the one that looks finished too early. Adding k for every window that qualifies is quick to write, and it is wrong as soon as two windows overlap.
Exercise · Break it: Windows counted twice. This counts the minutes inside surge windows: k minutes in a row whose requests total at least the threshold. It adds k for every window that qualifies. Find an input where it counts a minute twice.
def surging_minutes(demand, k, threshold):
total = 0
surging = 0
for i in range(len(demand)):
total += demand[i]
if i >= k:
total -= demand[i - k]
if i >= k - 1 and total >= threshold:
surging += k
return surgingSurge is about minutes: adding k for every qualifying window counts overlapping minutes twice.
The courier problem hides its mistake in one line: the minute at which a trip leaves. The trips are found one at a time, starting from the order that expires first.
Exercise · Find the bug: The minute a trip leaves. Each order can be collected at any minute from ready to latest. A trip leaves at one minute and takes up to capacity orders whose window holds that minute. This counts the trips but sometimes returns too many. Which line is wrong?
def min_trips(ready, latest, capacity):
n = len(ready)
order = sorted(range(n), key=lambda i: latest[i])
done = [False] * n
trips = 0
for first in range(n):
if done[order[first]]:
continue
t = ready[order[first]]
taken = 0
for j in range(first, n):
i = order[j]
if taken < capacity and not done[i] and ready[i] <= t <= latest[i]:
done[i] = True
taken += 1
trips += 1
return tripsLeave as late as the most urgent order allows: more orders are ready, and none on the trip expires earlier.
All three problems are medium, and together they make one core round. Each time box is close to what an interviewer allows for a medium problem, with time to explain.
Solve them in this order. The first is one pass with a running total, the second adds a rule you must defend, and the third adds a heading to the state.
Core problems invite a bigger follow-up. The interviewer changes one rule and asks whether your idea still holds. Prepare one for each problem.
Treat the three problems as one session: 85 minutes in all, with a timer. Work out loud, and explain each choice as if someone asked "Why?"
If a problem takes longer than its box, finish it anyway, but note the overrun.
Checkpoint · Mobility and delivery core round
Q1/5 Route A is RURU and route B is RRRRUU. The first move is free and each change of direction is a turn. Which route has fewer turns?
Answer: B
RURU changes direction 3 times and RRRRUU only once. Counting cells would pick the wrong route, so the search counts turns.
Q2/5 Demand is 5, 5, 5 and 0 over four minutes. A surge window is 2 minutes with at least 10 requests. How many minutes are surging?
Answer: A
Minutes 0 and 1 total 10, and so do minutes 1 and 2. Together they cover 3 minutes. Adding 2 for each window gives 4, because minute 1 is counted twice.
Q3/5 Why should a trip leave at the last minute of the most urgent order?
Answer: B
Every order on the trip is ready by that minute and lasts at least as long as the urgent one. Moving the trip later never loses an order.
Q4/5 Two orders are ready at minutes 0 and 5 and both last until minute 10. A trip holds two orders. How many trips are needed?
Answer: A
A trip at minute 10 finds both orders ready. Leaving as soon as the first order is ready needs 2 trips.
Q5/5 What must the state of a fewest-turns search on a grid hold?
Answer: B
A move is a turn only when it differs from the direction you arrived in, so the heading is part of the state. The step count does not price a turn.
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.