Start learning free

LESSON 329 OF 333 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Online Auction in Swift

Practice O(1)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you build an auction house. Sellers put items up for sale. Each item goes to the highest bid made before its end time.

Implement two classes:

  • Auction(seller, minPrice, endTime) holds one item for sale. It starts open, with no bids.
  • The auction's bid(user, amount, time) records a bid if the rules below allow it. It returns true if it did.
  • The auction's close(time) closes the auction if it may close now. It returns true if it did.
  • AuctionHouse() starts with no items.
  • open(item, seller, minPrice, endTime) puts an item up for sale. It returns false if an item with that name was opened before.
  • bid(user, item, amount, time) places a bid on the item. It returns true if the bid is accepted.
  • highest(item) returns the highest bid so far, such as "Ana 120".
  • close(item, time) closes the item. It returns the winner's name.
  • wins(user) returns how many items user has won.
  • earnings(seller) returns the sum of the winning bids on the seller's items.

A bid is accepted only when all of these hold:

  • The item was opened and isn't closed.
  • time is before the item's end time.
  • user isn't the item's seller.
  • amount is at least the item's minimum price.
  • amount is more than the highest bid so far.

A refused bid returns false and changes nothing.

highest returns the user and the amount, with one space between them. It returns "none" when the item has no bids or doesn't exist. It still works after the item closes.

close needs three things. The item was opened, it isn't closed yet, and time is at least its end time. Then the item closes, and the highest bidder wins it.

The winner's count of wins goes up by 1. The winning bid is added to the seller's earnings.

If nobody bid, the item still closes, and close returns "none". When the item can't close, close returns "none" and changes nothing.

Someone who never won an item has 0 wins. Someone who never sold an item has earned 0.

The calls run in order on one new AuctionHouse. Each call writes one line of output. Booleans are written as True and False.

 

Example 1:

Input:
AuctionHouse()
1. open("lamp", "Ana", 50, 100)
2. bid("Ben", "lamp", 60, 10)
3. bid("Cy", "lamp", 60, 20)
4. bid("Cy", "lamp", 75, 30)
5. highest("lamp")
6. close("lamp", 99)
7. close("lamp", 100)
8. wins("Cy")
9. earnings("Ana")
Output:
True
True
False
True
Cy 75
none
Cy
1
75
Explanation:
Ana sells a lamp. The minimum price is 50, and the sale ends at time 100.
Ben bids 60 at time 10.
Cy's bid of 60 is refused, because it doesn't beat 60.
Cy's bid of 75 is the new highest bid.
At time 99 the lamp can't close yet.
At time 100 it closes, and Cy wins it.
Cy has won 1 item, and Ana has earned 75.

Example 2:

Input:
AuctionHouse()
1. open("vase", "Dee", 20, 50)
2. bid("Dee", "vase", 30, 1)
3. bid("Eve", "vase", 15, 2)
4. bid("Eve", "vase", 20, 3)
5. bid("Fay", "vase", 20, 4)
6. bid("Fay", "vase", 21, 49)
7. bid("Eve", "vase", 40, 50)
8. bid("Eve", "bike", 40, 50)
9. highest("vase")
Output:
True
False
False
True
False
True
False
False
Fay 21
Explanation:
Call 2 fails, because Dee is the seller.
Call 3 fails, because 15 is below the minimum price.
Call 4 works. A bid equal to the minimum price is enough.
Call 5 fails, because 20 doesn't beat the highest bid.
Call 6 works at time 49, just before the end.
Call 7 fails, because time 50 is the end time.
Call 8 fails, because nobody opened a bike.

Example 3:

Input:
AuctionHouse()
1. open("book", "Gus", 10, 30)
2. open("book", "Hal", 5, 40)
3. bid("Ivy", "book", 12, 10)
4. close("desk", 30)
5. close("book", 30)
6. close("book", 35)
7. open("pen", "Hal", 5, 40)
8. close("pen", 40)
9. wins("Ivy")
10. earnings("Gus")
Output:
True
False
True
none
Ivy
none
True
none
1
12
Explanation:
Call 2 fails, because the name book is taken.
Call 4 returns none, because there is no desk.
Call 5 closes the book, and Ivy wins it.
Call 6 returns none, because the book is already closed.
Nobody bid on the pen, so call 8 returns none.
The pen is closed all the same.
Ivy has won 1 item, and Gus has earned 12.

 

Constraints:

  • An item name has 1 to 10 lowercase English letters.
  • A person's name has 1 to 10 English letters.
  • A person's name starts with a capital letter, and its other letters are lowercase.
  • Each minPrice and amount is a whole number from 1 to 10,000.
  • Each time is a whole number from 0 to 1,000,000,000.
  • The time of a bid or close call is never earlier than in the call before it.
  • At most 100,000 calls follow the constructor.

Note:

Each call should run in O(1) time. An auction keeps only its best bid, not every bid. The house finds each auction by name in a map, and keeps running totals of wins and earnings.

STEP 1 OF 37
    Ctrl + Enter to check

    Online Auction in Swift

    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free