Ad Auction: Second Price

10pts
Google

Problem Statement

Google uses a variation of the Second-Price Auction model. In this model, the highest bidder wins the ad slot, but the price they pay is the bid amount of the second-highest bidder.

Write a query to identify the winner of each auction and the price they must pay. If multiple bidders have the same bid amount and it is the highest, the "second price" is still that same amount. If there is only one bidder for a slot, the price paid is 0 (assume no reserve price).

Rules:

  • Output columns: ad_slot_id, winner_id, price_paid.
  • The winner_id is the user_id with the highest bid.
  • In case of a tie for the highest bid, the user_id with the lowest numerical value is the winner.
  • price_paid is the bid_amount of the second-highest bidder.
  • Result must be sorted by ad_slot_id ascending.
Tests your understanding of
Window Functions, Ranking and Marketplace Logic

Input Tables

bids
bid_id(INTEGER)ad_slot_id(INTEGER)user_id(INTEGER)bid_amount(DECIMAL)
11001105.5
21001114.25
31001126
410022010
510022110
61003302.5
71004401
81004410.5

Expected Output

ad_slot_id(INTEGER)winner_id(INTEGER)price_paid(DECIMAL)
1001125.5
10022010
1003300
1004400.5

Tags

HardWindow FunctionsRankingMarketplace Logic
25-35 min
45%

Hints