Design an Online Auction System
🔨 What is an online auction system? It is the software behind a site where sellers list items and buyers bid on them. Each auction has a starting price and an end time. Every new bid must beat the current highest one, and when the clock runs out, the highest bidder wins the item.
This problem looks smaller than it is. The bid rules fit on an index card, but interviewers push on two harder points: closing each auction at exactly the right moment, and staying correct when many people bid on the same item in the same second. A good answer gets the simple rules right quickly and spends its time on those two.
Requirements
"Design an online auction system. Sellers list items, buyers place bids, and the highest bidder wins when the auction ends."
As usual, the rules are yours to discover. Each answer below becomes something we code against.
Clarifying Questions
You: What makes a bid valid?
Interviewer: The first bid must be at least the starting price. Every later bid must be strictly higher than the current highest bid. If two people bid the same amount, the earlier one stays on top. Sellers cannot bid on their own items.
You: Can the current highest bidder raise their own bid?
Interviewer: Yes. It is just another bid that must beat the current highest one.
You: When does an auction end?
Interviewer: The seller picks a duration when listing. The auction is open from that moment until the end time. A bid that arrives exactly at the end time is too late.
You: Who closes an auction? Should we start a timer?
Interviewer: Every call passes the current time in, and times never go backwards. The system must behave as if every auction closed exactly at its end time, whichever call comes next.
You: What happens when an auction closes?
Interviewer: If it has bids, it is sold to the highest bidder and the item goes on that bidder's list of won items. If nobody bid, it closes unsold.
You: Do we handle payment, reserve prices, or automatic bidding up to a maximum?
Interviewer: No payment. The rest may come up later.
You: Will many people bid at the same time?
Interviewer: On a popular item, absolutely. The tests call your code one call at a time, but I want to hear how you would keep it correct with many threads.
The fourth answer shapes the design more than any other. "Behave as if it closed on time" means we cannot wait for someone to look at an auction before closing it, because closing has a side effect: the winner's list of items changes. We will see why that rules out the obvious lazy check.
💡 Interview tip: When a problem has deadlines, ask how time reaches your code. "Time is passed in" makes everything deterministic and testable. Then say how production differs: a scheduler calls the same close method with the real clock.
Final Requirements
- A seller creates an auction with an item name, a starting price and a duration. Auction ids are
1, 2, 3, .... A non-positive price or duration is rejected. - An auction is open from its creation time until
creation time + duration, not including the end time itself. - A bid is accepted only while the auction is open, only if the bidder is not the seller, and only if it is at least the starting price (first bid) or strictly higher than the current highest bid (later bids).
- When an auction's end time arrives, it closes: sold to the highest bidder, or unsold if it has no bids. Closing happens in end-time order, and the result is visible to every call made at or after the end time.
- Anyone can ask for an auction's current highest bid and, once it has closed, its winner.
- Anyone can ask for the items a bidder has won, in the order those auctions closed.
- Many bidders can bid on the same auction at once; the result must be as if the bids arrived one at a time.
Out of scope
- Payment, shipping and fees.
- Reserve prices, buy-it-now and automatic (proxy) bidding (see Extensibility).
- Cancelling an auction or retracting a bid.
- User accounts and search.