Inventory Allocation by Bid Priority
AmazonSoftware Engineer · Online AssessmentLast updated
Implement a tiered inventory allocation system that distributes limited stock among buyer bids based on price tiers and round-robin priority rules.
Problem Statement
An e-commerce platform needs to allocate a limited batch of inventory across multiple customer bids. Each bid is represented as [order_id, price, quantity, timestamp], where:
order_id: A unique positive integer identifying the bid.price: The unit price offered by the buyer.quantity: The number of units requested.timestamp: An integer timestamp indicating when the bid was placed.
You are given an integer total_inventory and a 2D array bids representing all submitted bids. Inventory must be allocated using the following rules:
- Price Priority: Bids with higher
pricemust be satisfied before bids with lowerprice. - Tier-Level Allocation:
- If the total remaining inventory is greater than or equal to the total quantity demanded by all bids at the current price tier, each bid in that tier is fully satisfied.
- If the remaining inventory is strictly less than the total demand at the current price tier, the remaining units are distributed among the bids in that tier in a round-robin fashion:
- Sort the bids in the current tier by
timestampascending. If timestamps are identical, break ties byorder_idascending. - While inventory remains, iterate through the sorted list of bids in that tier. In each round, allocate 1 unit to each bid that has not yet had its requested quantity fully satisfied.
- As soon as remaining inventory reaches 0, the allocation process terminates immediately.
- Sort the bids in the current tier by
Return a list of pairs [order_id, allocated_quantity] sorted by order_id in ascending order. Every submitted bid must appear in the output, even if it received 0 units.
Function Interface
def allocate_inventory(total_inventory: int, bids: list[list[int]]) -> list[list[int]]:
pass
Constraints
0 <= total_inventory <= 10^61 <= len(bids) <= 10^41 <= order_id <= 10^9, allorder_idvalues are distinct.1 <= price <= 10^61 <= quantity <= 10^51 <= timestamp <= 10^9
Examples
Example 1
Input:
total_inventory = 10
bids = [
[1, 100, 4, 10],
[2, 100, 5, 5],
[3, 50, 6, 2]
]
Output:
[[1, 4], [2, 5], [3, 1]]
Explanation:
- Price tier
100has bids1(demands 4) and2(demands 5). Total demand is 9, which is<= 10. Both are fully satisfied. Remaining inventory =10 - 9 = 1. - Price tier
50has bid3(demands 6). Only 1 unit remains, so bid3receives 1 unit. - Output is sorted by
order_id:[[1, 4], [2, 5], [3, 1]].
Example 2
Input:
total_inventory = 5
bids = [
[101, 80, 3, 20],
[102, 80, 2, 10],
[103, 80, 4, 30]
]
Output:
[[101, 2], [102, 2], [103, 1]]
Explanation:
- All bids are at price
80. Total demand is3 + 2 + 4 = 9 > 5, so round-robin allocation is used. - Sort order by timestamp: Bid
102(ts 10), Bid101(ts 20), Bid103(ts 30). - Round 1:
102gets 1 (rem 4),101gets 1 (rem 3),103gets 1 (rem 2). - Round 2:
102gets 1 (demands satisfied: 2/2, rem 1),101gets 1 (rem 0). - Inventory reaches 0. Allocation:
101-> 2,102-> 2,103-> 1. - Sorted by
order_id:[[101, 2], [102, 2], [103, 1]].
Edge Cases
total_inventory == 0: All bids receive0units.- Inventory runs out mid-round during round-robin.
- Multiple bids have both the same price and same timestamp.
Hints
Hint 1
Group the bids by price in descending order, and sort bids within the same tier by timestamp ascending, then order_id ascending.
Hint 2
If the sum of demands in the current tier exceeds remaining inventory, you do not need to simulate unit-by-unit; calculate how many complete rounds can be served before any bid caps out.
Hint 3
Ensure every input order_id is recorded in the final output array, even if its allocated units are 0, and sort the final result by order_id.
Follow-up questions
- How would you optimize the round-robin distribution if individual order quantities and total inventory are on the order of 10^12?
- If bids could be canceled or dynamically updated in real time via a streaming queue, what data structures would you use to maintain state?
- Suppose we want proportional allocation (weighted by bid price or demand) instead of round-robin when inventory is constrained. How does that change the time complexity and rounding guarantees?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.