Weighted Lottery Winners
Updated
Problem statement
Run a lottery that picks k different winners from a row of bidders. bids holds each bidder's bid, and a bigger bid gives a bigger share of each draw. Instead of random numbers, the draws use the given tickets, in order.
Draw d uses the ticket tickets[d] and works like this:
- Add up the bids of the bidders still in the lottery.
- Take the remainder of
tickets[d]divided by that total. - Walk through the remaining bidders from the left. Keep a running total of their bids, starting from zero.
- The first bidder whose running total goes above the remainder wins. Remove that bidder before the next draw.
Run k draws. Return the winners' indices in bids, in the order they were drawn.
Function
weightedWinners(bids: int[], tickets: int[], k: int) → int[]Examples
Example 1
bids = [1,3,6]tickets = [0,0]k = 2return = [0,1]In the first draw, the bids add up to ten. The first ticket is zero, so the remainder is zero. The first bidder's bid, 1, is already above zero, so the first bidder wins and is removed. In the second draw, the bids left add up to nine, and the second ticket is also zero. Now the first bidder still in the lottery is the second bidder, and its bid is above zero, so it wins. The winners, in draw order, are the first bidder and then the second bidder.
Example 2
bids = [1,3,6]tickets = [9,0]k = 2return = [2,0]In the first draw, the bids add up to ten, and the first ticket is nine, so the remainder is nine. The running totals are one, four and ten. Ten is the first total above nine, so the third bidder wins and is removed. Its index is 2. In the second draw, the bids left add up to four, and the second ticket is zero. The first bidder's running total is already above zero, so the first bidder wins. The winners, in draw order, are the third bidder and then the first bidder.
Constraints
1 <= bids.length <= 20000 <= bids[i] <= 100and at leastkbids are positive.0 <= k <= min(bids.length,tickets.length)- Tickets are nonnegative integers.
More Amazon Onsite Interview questions
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Calculate Server Shutdown PenaltyStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Instrument Stack Language JumpsJane Street · PHONE SCREENJane Street interview questions
- Schedule Buffered Video PlaybackNuro · PHONE SCREENNuro interview questions
- Hall of Shifting TilesAdobe · OAAdobe interview questions
- Prefix Scores With the Running MaximumC3.ai · OAC3.ai interview questions
- Online No-Intercept Linear RegressionTwo Sigma · OATwo Sigma interview questions
- Wallet Balance on a Selected DayBharatPe · PHONE SCREEN
- Array Challenge (QR Intern)Akuna Capital · OAAkuna Capital interview questions
- Range Negation UpdatesJP Morgan Chase · OAJP Morgan Chase interview questions
- Data UpdatesExpedia · OAExpedia interview questions