Call Auction Clearing Price and Order Fills
Updated
Problem statement
An exchange collects buy orders and sell orders and then runs a call auction: all trades happen at one single price, called the clearing price.
Each order is a pair [price, quantity]. buys[i] is the buy order with index i, and sells[j] is the sell order with index j. A smaller index means the order arrived earlier.
At a price p:
- A buy order can trade if its price is at least
p. - A sell order can trade if its price is at most
p. - The demand is the total quantity of the buy orders that can trade, and the supply is the total quantity of the sell orders that can trade.
- The traded amount is
min(demand, supply), and the imbalance is|demand - supply|.
Choosing the clearing price
Only prices that appear in at least one order (buy or sell) are tried. Among them, pick:
- the price with the largest traded amount (measured in quantity, not in price times quantity);
- if several prices tie, the one with the smallest imbalance;
- if they still tie, the lowest price.
Filling the orders
Let volume be the traded amount at the clearing price. On each side, hand out exactly volume units to the orders that can trade, in this order:
- Buys: higher price first. Equal prices go by smaller index.
- Sells: lower price first. Equal prices go by smaller index.
Each order takes as much of its quantity as is still left, so at most one order on each side fills only partly. Orders that cannot trade at the clearing price fill 0.
If the largest traded amount is 0, nothing trades: the price and the volume are both 0, and every fill is 0.
Return [[price, volume], buyFills, sellFills], where buyFills[i] is the filled quantity of buys[i] and sellFills[j] is the filled quantity of sells[j].
Function
runCallAuction(buys: int[][], sells: int[][]) → int[][]Examples
Example 1
buys = [[10,5],[12,3],[9,4]]sells = [[8,4],[11,6],[9,3]]return = [[10,7],[4,3,0],[4,0,3]]The candidate prices are the order prices: 8, 9, 10, 11 and 12.
| Price | Buy demand | Sell supply | Traded | Imbalance |
|---|---|---|---|---|
| 8 | 12 | 4 | 4 | 8 |
| 9 | 12 | 7 | 7 | 5 |
| 10 | 8 | 7 | 7 | 1 |
| 11 | 3 | 13 | 3 | 10 |
| 12 | 3 | 13 | 3 | 10 |
The most that can trade is 7, at price 9 and at price 10. Price 10 leaves a smaller imbalance (1 instead of 5), so the clearing price is 10 and the volume is 7.
- Buys: the buys at 12 and 10 can trade, and the buy at 9 cannot. The buy at 12 goes first and fills all 3. The buy at 10 gets the other 4 of its 5. So the buy fills are
[4, 3, 0]. - Sells: the sells at 8 and 9 can trade, and the sell at 11 cannot. Together they hold exactly 7, so both fill completely. So the sell fills are
[4, 0, 3].
Example 2
buys = [[5,10],[4,2]]sells = [[6,3],[7,2]]return = [[0,0],[0,0],[0,0]]Every buy price (5 and 4) is lower than every sell price (6 and 7), so at each candidate price either no buy or no sell can trade.
| Price | Buy demand | Sell supply | Traded | Imbalance |
|---|---|---|---|---|
| 4 | 12 | 0 | 0 | 12 |
| 5 | 10 | 0 | 0 | 10 |
| 6 | 0 | 3 | 0 | 3 |
| 7 | 0 | 5 | 0 | 5 |
The largest volume is 0, so there is no trade. The answer is [0, 0] followed by all-zero fills.
Example 3
buys = [[7,4],[7,4]]sells = [[5,2],[6,3]]return = [[6,5],[4,1],[2,3]]At price 5 only 2 can trade. At 6 and at 7, demand is 8 and supply is 5, so 5 can trade with an imbalance of 3 at both prices. That is a full tie, so the lower price, 6, wins.
- Buys: both buys have price 7, so the earlier one (index 0) goes first and fills all 4. The later one gets the last 1.
- Sells: both sells can trade at 6 and together hold exactly 5, so both fill completely.
Constraints
1 <= buys.length <= 10001 <= sells.length <= 1000buys[i].length == 2andsells[j].length == 21 <= buys[i][0], sells[j][0] <= 10^9(prices)1 <= buys[i][1], sells[j][1] <= 10^4(quantities)
More Hudson River Trading Phone Screen questions
- Format a Signed Integer with CommasPHONE SCREEN · Seen Nov 2026
- Order and Batch Open-Close LifecyclePHONE SCREEN · Seen Nov 2026
- Tilting Maze BallPHONE SCREEN · Seen Oct 2026
- Tiered Expiring Item StorePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Market Data Signal WatcherPHONE SCREEN · Seen Jul 2026
- Integer to String Without Built-insPHONE SCREEN · Seen May 2026
- Contact Tracing Through Timed MeetingsPHONE SCREEN · Seen Nov 2024
- Polydivisible Pandigital NumbersPHONE SCREEN · Seen Feb 2024
- Defragment a Buffer of ObjectsPHONE SCREEN · Seen Nov 2023
- Read N Bytes with read4096 Across CallsPHONE SCREEN · Seen Oct 2023
Similar questions at other companies
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Peak Chat Load Across AgentsCresta · PHONE SCREENCresta interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Remove the Fewest Magic BeansThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Majority Labels over Audio IntervalsNooks · PHONE SCREEN
- Aggressive CowsAmazon · PHONE SCREEN
- Minimize Range After Three ChangesDRW · PHONE SCREENDRW interview questions
- Maximize Compatible Car RidersUber Freight · PHONE SCREEN · ONSITE INTERVIEWUber Freight interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions