Three hard problems the way an e-commerce interview frames them: a fair flash sale queue, the most revenue with cooldowns, and the fastest warehouse route with a battery.
An e-commerce stretch round puts one hard rule on a tool you already know. The rule comes from the shop: a sale must be fair, a promotion tires customers, and a warehouse robot has a battery. The tool is a queue, a table or a graph search, and the rule changes what you must remember.
Three habits help.
These habits matter more than the code. A hard problem is mostly deciding the state and the order of work.
Exercise · Predict the output: Promotions with cooldowns. Four days earn 3, 4, 5 and 6 units, and a promotion on each day blocks the next 1, 0, 2 and 0 days. This fills a table from the last day back. What does it print?
revenue = [3, 4, 5, 6]
cooldown = [1, 0, 2, 0]
n = len(revenue)
best = [0] * (n + 1)
for i in range(n - 1, -1, -1):
resume = min(n, i + cooldown[i] + 1)
best[i] = max(best[i + 1], revenue[i] + best[resume])
print(best[0])Each problem is an Academy idea with a hard rule on top. Read the signal, then use the move.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| one unit a turn, then the back of the queue, and stock runs out | a queue simulation with a clock that jumps when nobody waits | Queue simulations and rounds |
| run a promotion or skip, and each one blocks the days after it | a table from the last day back: skip, or take and resume | 1-D DP: choices along a line |
| a limit that changes along the route, and the fastest trip | Dijkstra on pairs of place and charge left | Dijkstra's shortest paths |
Take a flash sale with 5 units, and three customers who arrive at second 0 and want 2, 3 and 1 units. In turns of one unit, customer 2 finishes at second 2 and customer 0 at second 3. Customer 1 gets 2 of the 3 units wanted before the stock ends.
The answers are 3, -1 and 2. If each customer were served completely in turn, the answers would be 1, 4 and -1.
For the warehouse, take a line of stations 0, 1, 2 and 3 with aisles of length 4. The battery holds 10 units, a charge takes 5 seconds, and only station 1 has a charger. Driving all three aisles takes 12 seconds, but 12 units of battery are needed, so the robot must charge.
The fastest trip is 17 seconds. With no charger the answer is -1.
Exercise · Predict the output: Charging only when needed. Aisles of length 4, 4 and 4 lead from station 0 to station 3, and the battery holds 10 units. Station 1 has a charger, and charging takes 5 seconds. This robot charges only when the next aisle does not fit. What does it print?
lengths = [4, 4, 4]
charger = [False, True, False]
battery = 10
charge_time = 5
charge = battery
time = 0
ok = True
for i in range(len(lengths)):
if lengths[i] > charge:
if charger[i]:
time += charge_time
charge = battery
else:
ok = False
break
time += lengths[i]
charge -= lengths[i]
print(time if ok else -1)A route with a battery needs the charge in the state, because charging early at a charger can be the only way to finish.
The flash sale has a simple trap in the queue itself. A customer who still wants units goes to the back, not the front.
Exercise · Find the bug: The back of the queue. One unit is sold each second. The customer at the front buys a unit and, if they want more, rejoins the queue. Everyone arrives at second 0. It sells the right number of units, but the finish seconds are wrong. Which line is wrong?
def finish_times(stock, wants):
left = list(wants)
finish = [-1] * len(wants)
queue = list(range(len(wants)))
t = 0
while stock > 0 and queue:
c = queue.pop(0)
left[c] -= 1
stock -= 1
if left[c] == 0:
finish[c] = t
else:
queue.insert(0, c)
t += 1
return finishThe cooldown problem looks like a greedy one, and it is not. The day with the biggest revenue may block better days.
Taking the first allowed promotion each time can earn less, because skipping a day may allow two better ones.
All three are hard. Each time box is what a strong candidate needs, and a real loop would give you one of them.
Solve them in this order. The queue adds a clock to a simulation you know, the promotions add a table that depends on the choice, and the warehouse adds a state.
A stretch problem is followed by a harder question. Prepare one for each.
The full set is 115 minutes, which is longer than any one interview. Use it as three sessions, one problem each, with a timer.
Write the table or the queue by hand for the statement's example before you type. The numbers you get are your tests.
Checkpoint · E-commerce stretch round
Q1/5 In the flash sale, a customer buys one unit and wants 2 more. Where do they go?
Answer: B
Fair turns mean everyone gets a unit before anyone gets a second one. A customer who still wants units waits behind the others.
Q2/5 A promotion on day 2 has a cooldown of 0 days. Which is the first day the next promotion may run?
Answer: B
The next promotion runs on day i + cooldown + 1, which is day 3. With no cooldown the next day is allowed, but the same day is not.
Q3/5 Why does taking the first allowed promotion each time fail on revenues 3, 4, 5 and 6 with cooldowns 1, 0, 2 and 0?
Answer: B
The first allowed day is day 0, then day 2, for 3 + 5 = 8. Skipping day 0 allows days 1 and 3 for 4 + 6 = 10.
Q4/5 Why is the station alone not a good state for the warehouse route?
Answer: B
The robot's options depend on the charge left. The same station reached with 2 units or with 10 units leads to different trips, so both belong in the state.
Q5/5 The queue is empty, and the next customer arrives at second 40. What does the simulation do with the clock?
Answer: B
Nothing is sold while nobody waits, so the clock can jump straight to the next arrival. Stepping through empty seconds is correct but slow.
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.