FastPrepUnfulfilled Bids after Ranked Round-Robin Allocation

Unfulfilled Bids after Ranked Round-Robin Allocation

Amazon logoAmazon● MediumCONTRACTOA
Learn

Problem statement

You receive a fixed inventory and a list of customer bids. Each bid is represented as [customerId, bidAmount, quantity, bidPlacedAt].

Allocate inventory using these rules:

  1. Process higher bidAmount values before lower ones.
  2. Within one bid amount, order bids by increasing bidPlacedAt, breaking a remaining tie by increasing customerId.
  3. Allocate one unit to each still-unfulfilled bid in that order, then repeat another cycle for the same bid amount.
  4. Move to the next lower bid amount only after every bid in the current group is fulfilled or the inventory is exhausted.

Return the customerId of every bid that receives fewer units than its requested quantity. Return the IDs in increasing order.

Function

findUnfulfilledBids(bids: int[][], totalInventory: int) → int[]

Examples

Example 1

bids = [[1,3,1,1],[2,4,2,2]]totalInventory = 1return = [1,2]

Customer 2 receives the only unit because its bid amount is higher. Customer 1 receives zero of one requested unit, and customer 2 receives one of two requested units, so both bids are not fully fulfilled.

Example 2

bids = [[1,3,2,1],[2,3,2,2],[3,3,1,3]]totalInventory = 4return = [2]

The first cycle gives one unit to customers 1, 2, and 3. The remaining unit goes to customer 1 at the start of the second cycle. Only customer 2 receives fewer units than requested.

Constraints

  • 1 <= bids.length <= 2000
  • Every bid has exactly four integer fields.
  • 1 <= customerId, bidAmount, quantity, bidPlacedAt <= 10^9
  • All customerId values are distinct.
  • The sum of all requested quantities is at most 200000.
  • 0 <= totalInventory <= 200000

More Amazon problems

See Amazon hiring insights
public int[] findUnfulfilledBids(int[][] bids, int totalInventory) {
    // Write your code here.
}
bids[[1,3,1,1],[2,4,2,2]]
totalInventory1
expected[1,2]
Checking account…