FastPrepLimit Order Matching

Limit Order Matching

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process the finite orders batch 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.

For this exercise, assume limit orders for one instrument. 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.

These rules are explicit practice conventions for the reported matching objective. Cancellation, replacement, market orders, fees, multiple instruments, final-book reporting and the unreported interview-specific conditions 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 buy0at10. The next sell fills its remaining four units and rests with two; the last buy consumes those two at resting price10.

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 sell35before sell40. The second fill is partial and uses the resting sell40price.

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 at21does not cross buys at20. The later sell at20fills earlier buy0before buy1; identical price does not erase arrival priority.

Constraints

  • 0 <= orders.length <= 2000.
  • Each row contains exactly three integers and side is0or1.
  • 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 signed32-bit integer.
  • Prices are integer price units. No price-times-quantity amount is requested.
  • The interface, valid-input convention, numeric envelope and matching policy are authored practice assumptions.

More Headlands Technologies problems

See Headlands Technologies hiring insights
public int[][] matchLimitOrders(int[][] orders) {
    // write your code here
}
orders[[0,10,7],[1,8,3],[1,10,6],[0,12,2]]
expected[[0,1,10,3],[0,2,10,4],[3,2,10,2]]
Checking account…