Three easy problems the way an e-commerce interview frames them: orders grouped by shipping zone, the first shipment that covers a back-order, and a cart total with a free item in every bundle.
An e-commerce company sells things: it has catalogues, carts, orders, stock and warehouses. Its coding interviews use the same data structures as any other, and the stories are about shopping. A warm-up problem might group orders for a truck, answer a back-order or total a cart.
The interviewer listens for four habits.
The warm-up round has three easy problems. Each one hides a single boundary or choice that the statement gives once.
Exercise · Predict the output: The free item in each bundle. A bundle has 2 items, and the cheapest item of a bundle is free. The cart holds items priced 5, 9, 3, 7 and 6. This sorts from the dearest and makes every second item free. What does it print?
prices = [5, 9, 3, 7, 6]
group = 2
prices.sort(reverse=True)
total = 0
for p in prices:
total += p
for i in range(group - 1, len(prices), group):
total -= prices[i]
print(total)Each problem is one Academy idea. Read the signal in the statement, then use the move.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| orders that share a key cut from a string, groups in order | a map from key to the position of its group | Designing with hash maps |
| the first position that reaches a value, in a list that never goes down | a lower bound with a half-open range | Lower and upper bound |
| the cheapest item of each bundle is free, and you choose the groups | sort from the dearest, and take every group-th item | Comparators in four languages |
What changes here is the frame. A zone key is cut from a postcode, and a postcode shorter than the prefix is its own zone.
The back-order search must return the first position when totals repeat, and -1 when no shipment reaches the order. The bundle problem is a greedy choice. Its proof is one sentence: the free item is the cheapest of its group, so grouping the dearest items together wastes the least.
Take shipment totals 0, 4, 4, 9 and 12, and back-orders for 1, 4, 5, 12, 13 and 0. The answers are 1, 1, 3, 4, -1 and 0. An order for 4 units is first covered at position 1, not 2, and an order for 0 units is covered at position 0.
Exercise · Predict the output: Zones from short postcodes. The zone of an order is the first 2 characters of its postcode, or the whole postcode if it is shorter. The postcodes are SW1A, E14, SW3X, E1 and W. How many zones does this print?
postcodes = ["SW1A", "E14", "SW3X", "E1", "W"]
prefix = 2
zones = set()
for code in postcodes:
zones.add(code[:prefix])
print(len(zones))The zone is the first characters of a postcode, a short postcode is its own zone, and zones keep their order of first appearance.
The back-order search has a boundary trap that is easy to miss: what the search returns when nothing reaches the order.
Exercise · Find the bug: No shipment reaches the order. This returns the first shipment whose running total reaches an order, in a list that never goes down. It is right whenever some shipment reaches the order. Which line is wrong?
def first_covering(totals, need):
lo = 0
hi = len(totals)
while lo < hi:
mid = (lo + hi) // 2
if totals[mid] >= need:
hi = mid
else:
lo = mid + 1
return loThe bundle problem has a different trap: the choice of groups. Pair the items in cart order, and the customer pays more than necessary.
Grouping items in cart order wastes the free items; sort from the dearest so the dearest items share a bundle.
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 zones are a map, the back-orders add a binary search, and the bundles add a greedy choice with a reason.
A warm-up problem rarely ends at the first answer. The interviewer changes one rule and asks what changes in your code. Prepare one follow-up for each.
Treat the three problems as one interview of 60 minutes, with a timer. Work out loud, as if someone is listening.
Stop at each box. If a problem overruns, note which step took the time.
Checkpoint · E-commerce warm-up round
Q1/5 How should a cart total be stored when prices are whole units and carts can be large?
Answer: B
Prices are whole units, so integers are exact. A total over many items can pass 32 bits, so the total uses 64 bits.
Q2/5 Orders arrive for zones A, B, A. Which lists does the answer hold?
Answer: B
Zones appear in the order they were first seen, and the orders of a zone keep their arrival order.
Q3/5 A postcode has 1 character and the zone is the first 2 characters. What is its zone?
Answer: B
A postcode shorter than the prefix is its own zone, so the code must not fail on a short string.
Q4/5 Totals are 0, 4, 4, 9 and 12. Which position first covers an order for 4 units?
Answer: B
The first position whose total is at least 4 is position 1. Position 2 also has a total of 4, but the question asks for the first.
Q5/5 Why sort from the dearest item in the bundle problem?
Answer: B
Grouping the dearest items together wastes the least on free items. Sorted that way, every group-th item is the free one.
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.