Design an online auction
hard 90 minDesign the classes of an auction with maximum bids, a secret reserve, steps and a soft close.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
One owner for one lot: calls travel, fields change, and the matching lines light up.
Operations
The price rule
Start 100, reserve 150, step 10. Seven bids, every branch.
Step 1 of 7
First bid
| Bid | Leads | Their maximum | Price | |
|---|---|---|---|---|
| 1 | Asha 140 | Asha | 140 | 100 |
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 |
Asha allows 140. The price is only the start: 100. The reserve is not reached.
Step 1: First bid
def _settle(self, bid): best = self._best if best is None: self._best, self._price = bid, self._lot.start elif bid.bidder == best.bidder: self._best = bid elif bid.maximum > best.maximum: self._best, self._price = bid, self._just_above(best.maximum, bid.maximum) else: self._price = self._just_above(bid.maximum, best.maximum) if self._best.maximum >= self._lot.reserve: self._price = max(self._price, self._lot.reserve)def _just_above(self, loser, winner): return min(winner, loser + self._rules.step(loser))- Cost
- O(1) for each bid
- Guarantee
- The price never falls
- Robots in steps of 10
- many rounds, many races
Strictly greater takes the lead. Equal only pushes.
In the real world
The same online auction doing real jobs. Pick one and step through it.
Sam wants to know Lee's maximum.
Step 1 of 3
180
Outbid, price 190. So Lee allows at least 190.
Step 1: 180
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Check, then set
criticalTwo bids pass the check. The lower one may write last, or price and leader come from different bids.
Wrongif amount > item.highest: item.highest = amount item.leader = whoRightwith self._lock: self._check(...) self._settle(bid) # price and leader togetherThe clock before the lock
criticalA bid waits past the end with an old time.
Wrongat = now()with self._lock: ...Rightwith self._lock: at = now()A timer that closes
majorIt knows the first end only, and bids arrive until it wakes up.
WrongTimer(600, auction.set_closed).start()Rightview = auction.close(now) # may say: not yetTelling under the lock
criticalA slow screen holds up the sale. One that bids back deadlocks.
Wrongwith self._lock: ... for l in listeners: l(self)Rightanswer = auction.place(...) # lock is free againfor l in listeners: l(answer.view)Outbid as an error
minorThe bid counted and raised the price. The page shows that nothing happened.
Wrongraise OutbidException()Rightreturn Answer(number, leading=False, view=view)Secrets with getters
majorA serializer prints every getter, and the maximum is on the page.
Wrongclass Auction: def getHighestMaximum(self): ...Rightclass View: # price, leader, minimum, end, version
Interview tips
Open with
- Price or maximum?
- Secret reserve?
- How does it end?
Draw
- One owner for each lot.
- Lot and Bid frozen.
- View without secrets.
Prove it
- A tie.
- The cap.
- The last second, both orders.
Keep outside
- Screens.
- Mail.
- Money.