Limit Order Matching
Updated
Problem statement
Process the rows of orders in arrival order. Every row is [side,price,quantity], where side == 0 means BUY and side == 1 means SELL. An order's unique identity is its zero-based original row index.
In this exercise, all orders are limit orders for the same instrument. A resting order is an earlier order still waiting to be filled. An incoming BUY may match resting SELL orders priced at or below its limit. An incoming SELL may match resting BUY orders priced at or above its limit. Choose the best opposite price first: the lowest SELL price or highest BUY price. At equal price, fill the resting order with the earliest original index first.
Each fill uses the resting order's price and the smaller remaining quantity. Keep matching an incoming order until it is filled or no eligible opposite order remains. Its unfilled remainder then rests at its own limit price, preserving its original arrival priority. A partially filled resting order keeps its priority.
Return each fill in execution order as [buyOriginalIndex,sellOriginalIndex,restingPrice,filledQuantity]. Orders that produce no fills add no output row. Empty input returns an empty matrix. Do not modify orders.
Cancellation, replacement, market orders, fees, multiple instruments and final-book reporting are outside this exercise.
Function
matchLimitOrders(orders: int[][]) → int[][]Examples
Example 1
orders = [[0,10,7],[1,8,3],[1,10,6],[0,12,2]]return = [[0,1,10,3],[0,2,10,4],[3,2,10,2]]The first sell fills three units of resting buy 0 at 10. The next sell fills its remaining four units and rests with two; the last buy consumes those two at resting price 10.
Example 2
orders = [[1,40,2],[1,35,3],[0,42,4]]return = [[2,1,35,3],[2,0,40,1]]The incoming buy chooses lower sell 35 before sell 40. The second fill is partial and uses the resting sell 40 price.
Example 3
orders = [[0,20,2],[0,20,3],[1,21,1],[1,20,4]]return = [[0,3,20,2],[1,3,20,2]]The sell at 21 does not cross buys at 20. The later sell at 20 fills earlier buy 0 before buy 1; identical price does not erase arrival priority.
Constraints
0 <= orders.length <= 2000.- Each row contains exactly three integers and
sideis0or1. 1 <= price,quantity <= 1000000000.- Inputs are valid. Each fill quantity is positive and at most an input order's quantity; every result field fits a signed
32-bit integer. - Prices are integer price units. No price-times-quantity amount is requested.
More Headlands Technologies Phone Screen questions
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
- Pearson Correlation CoefficientPHONE SCREEN · Seen Apr 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
- Queue Mean from Corrective SnapshotsPHONE SCREEN · Seen Mar 2026
- Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
- CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
- Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
- Top Trade Events by NotionalPHONE SCREEN · Seen May 2024
Similar questions at other companies
- Batch API Requests with Retry DelaysDecagon · PHONE SCREENDecagon interview questions
- Combine the Two Smallest Until All Reach kAdobe · PHONE SCREENAdobe interview questions
- Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Price-Time Order MatchingTwo Sigma · PHONE SCREENTwo Sigma interview questions
- Ad Score Scheduler With DelayGoogle · ONSITE INTERVIEWGoogle interview questions
- CPU Scheduling SimulatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Named Async Task SchedulerAirwallex · OAAirwallex interview questions
- Dynamic Batch Decode TracexAI · ONSITE INTERVIEWxAI interview questions
- Financial Account LedgerStripe · OA · PHONE SCREENStripe interview questions
- Paged KV Cache AccessNVIDIA · PHONE SCREENNVIDIA interview questions