Three medium problems the way an e-commerce interview frames them: products bought together, seconds a product is oversold, and the longest stretch around each price in which it is lowest.
The warm-up round tested single boundaries. An e-commerce core round asks you to count the right thing, and to be exact about what the right thing is. The problems are medium, and the stories come from the shop: related products, stock held for checkout, and price history.
Three habits come up again and again.
Say the set you count over, the ends of every interval, and the rule for equal values before you write a line.
Exercise · Predict the output: Seconds over the stock. A product has stock 3. Three reservations hold units: 3 units from second 0 to 5, 4 units from second 5 to 8, and 2 units from second 6 to 10. A reservation ends before the second it names. How many seconds are oversold?
starts = [0, 5, 6]
ends = [5, 8, 10]
units = [3, 4, 2]
stock = 3
events = []
for i in range(len(starts)):
events.append((starts[i], units[i]))
events.append((ends[i], -units[i]))
events.sort()
reserved = 0
over = 0
prev = 0
for time, change in events:
if reserved > stock:
over += time - prev
prev = time
reserved += change
print(over)Each problem is an Academy idea. The new part is the set you count over or the end of an interval.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| products in the same basket as a target, ranked | a count for each other product, over baskets that hold the target | Designing with hash maps |
| units held for a period, and time spent over the stock | events sorted by time, a running total, and the time between events | Intervals |
| the longest run around each price in which it is lowest | the closest lower price on each side, from a monotonic stack | Monotonic stacks |
Take four baskets: 1, 7, 3 and 1, 3, 7, 9 and 2, 3 and 1, 9. The target is product 1, and only the first, second and fourth baskets hold it. Products 7, 3 and 9 each share a basket with product 1 twice, so the ranking by count is a tie. The smaller ids come first: 3, 7, 9.
Product 2 never shares a basket with product 1, and the third basket does not count at all.
For the prices 4, 2, 2, 5, 1 and 3, the longest stretch around each day has no lower price inside it. The lengths are 1, 4, 4, 1, 6 and 1. The two days with price 2 share one stretch of 4 days, because equal prices do not end it. If every price is 3, every day has a stretch of 3.
Exercise · Predict the output: A stretch with an equal price. A stretch around a day holds consecutive days with no price lower than the day's own price. This measures the stretch around day 2 in the prices 4, 2, 2, 5, 1 and 3. What does it print?
prices = [4, 2, 2, 5, 1, 3]
day = 2
left = day
right = day
while left > 0 and prices[left - 1] >= prices[day]:
left -= 1
while right < len(prices) - 1 and prices[right + 1] >= prices[day]:
right += 1
print(right - left + 1)Sort the start and end events, keep a running total, and add the time between events that was spent over the stock.
The recommendation problem has a ranking trap. Equal counts are common, and the statement gives the order for them.
Exercise · Find the bug: Most together first. This ranks the products that share a basket with the target, and returns the first k. Product ids are below 100. It is right when the counts are all equal, but wrong otherwise. Which line is wrong?
def bought_together(baskets, target, k):
together = [0] * 100
for basket in baskets:
if target in basket:
for product in basket:
if product != target:
together[product] += 1
ranked = [p for p in range(100) if together[p] > 0]
ranked.sort(key=lambda p: (together[p], p))
return ranked[:k]The price problem needs the closest lower price on each side, and a stack gives both in one pass.
An equal price does not end a stretch, so compare strictly: only a lower price stops it.
The three problems are medium. Each time box is what an interviewer allows for a medium problem, with time to explain and test.
Solve them in this order. The baskets add a rule to a count, the reservations add time to a sort, and the prices add a stack.
A medium problem rarely ends at the first answer. Prepare one follow-up for each.
Treat the three problems as one session of 80 minutes, with a timer. A real core round often has two of them in 45 minutes, so the whole set is a hard rehearsal.
Stop each problem at its box, and note what was left.
Checkpoint · E-commerce core round
Q1/5 To find products bought together with product 1, which baskets are counted?
Answer: B
The question is about products that share a basket with the target. A basket without the target says nothing about it.
Q2/5 Products 3 and 7 each share 2 baskets with the target. Which is recommended first?
Answer: A
Equal counts go to the smaller id. Following the order of the data would make the answer change with the input.
Q3/5 Events happen at seconds 2 and 6, and the total is 5 between them. The stock is 4. How many oversold seconds do those two events cover?
Answer: B
The time from second 2 to second 6 was spent at a total of 5, which is over the stock, so the 4 seconds between the events are oversold.
Q4/5 One reservation ends at second 5 and another starts at second 5. Do they overlap?
Answer: B
A reservation holds its units up to, but not including, its end. At second 5 the first has ended, so the second starts alone.
Q5/5 A stretch around a day holds no price lower than that day's price. Does an equal price end the stretch?
Answer: B
The stretch may contain days with an equal price. Only a price that is strictly lower than the day's own price ends it.
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.