AlgoViz

Design an online auction

hard 90 min

Design 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

0:00 / 5:05

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

BidLeadsTheir maximumPrice
1Asha 140Asha140100
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

sambidderbid180learns190 or morecowAuctionbestlee ???price190end650lockfreescreenListenershows190place(180)outbid, 190

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

    critical

    Two bids pass the check. The lower one may write last, or price and leader come from different bids.

    Wrong
    if amount > item.highest:    item.highest = amount    item.leader = who
    Right
    with self._lock:    self._check(...)    self._settle(bid)      # price and leader together
  • The clock before the lock

    critical

    A bid waits past the end with an old time.

    Wrong
    at = now()with self._lock: ...
    Right
    with self._lock:    at = now()
  • A timer that closes

    major

    It knows the first end only, and bids arrive until it wakes up.

    Wrong
    Timer(600, auction.set_closed).start()
    Right
    view = auction.close(now)     # may say: not yet
  • Telling under the lock

    critical

    A slow screen holds up the sale. One that bids back deadlocks.

    Wrong
    with self._lock:    ...    for l in listeners: l(self)
    Right
    answer = auction.place(...)   # lock is free againfor l in listeners: l(answer.view)
  • Outbid as an error

    minor

    The bid counted and raised the price. The page shows that nothing happened.

    Wrong
    raise OutbidException()
    Right
    return Answer(number, leading=False, view=view)
  • Secrets with getters

    major

    A serializer prints every getter, and the maximum is on the page.

    Wrong
    class Auction:    def getHighestMaximum(self): ...
    Right
    class 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.