FastPrepMatch Orders by Price and Arrival Time
Problems/Match Orders by Price and Arrival Time

Match Orders by Price and Arrival Time

Akuna Capital logoAkuna Capital● MediumNEW GRADFULLTIMEOAPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: Fuller reports describe live order matching, price priority, FIFO at tied prices and cancellation. Another gives an Order structure and asks for trades as each new order arrives. One phone recollection only tentatively names order-book maintenance and a heap; ordinary matching is the practice interpretation for that brief account, rather than a claim that the phone round specified matching or cancellation. A newer report of a 24-hour take-home for a Shanghai quantitative developer role describes an order matching system with buy, sell, cancel, modify and print operations, but gives no rules for them.

Crossing, partial fills, trade price, ID rules, cancellation edge cases, bounds and the batch interface are authored details. Practice choices for modify: a modify keeps the side, replaces the price and the remaining quantity, loses the order's queue position and is matched at once like a new arrival, and modifying an order that is not resting does nothing. Printing the book is not judged here: the function returns trades only, and the editorial explains how a print would read the book.

The judged core task matches the fuller reports at about 85% and the take-home report at about 70%.

Maintain an order book while processing commands in order. Start with an empty book. An order has an ID, a side, a limit price and a remaining quantity.

Each command has one of these forms:

  • ADD: [0,id,side,price,quantity], where side is 0 (buy) or 1 (sell), and id, price and quantity are positive.
  • CANCEL: [1,id,0,0,0], with a positive id.
  • MODIFY: [2,id,0,price,quantity], with a positive id, a new positive price and a new positive remaining quantity.

Process an ADD completely before moving to the next command:

  1. An incoming buy can match a resting sell whose price is at most its buy limit. Choose the lowest sell price first. An incoming sell can match a resting buy whose price is at least its sell limit. Choose the highest buy price first.
  2. If several resting orders have the same price, choose the one that entered the book earliest. A partial fill keeps that position.
  3. Fill the smaller remaining quantity of the two orders. The execution price is the price of the resting order, which was already in the book. Record one trade and reduce both quantities.
  4. Remove a fully filled order. Continue matching while the incoming order has quantity and an eligible opposite order exists. Keep any unmatched incoming quantity in the book at its limit price.

A CANCEL removes only the named order's unfilled remainder. It never changes earlier trades. Cancelling an unknown, fully filled or already cancelled order does nothing. Cancelling an ID that has never been added does not prevent a later ADD with that ID.

A MODIFY changes an order that is still resting in the book. Its side stays the same, its limit price becomes the new price, and its remaining quantity becomes the new quantity (it replaces the old remainder; it is not added to it). The order loses its place in the queue: it leaves the book and is processed at once like a new incoming order arriving at this command, so it can trade right away, and any rest goes to the back of the queue at its new price. Trades it makes use its ID as before. Modifying an unknown, fully filled or cancelled order does nothing.

Return all trades as rows [buyId,sellId,executionPrice,filledQuantity] in execution order. Keep the matches from one incoming or modified order before those from the next command. Do not sort or combine trade rows. If no trade occurs, return an empty array.

Unmatched orders remain until filled or cancelled. Do not match against future commands. Do not change commands; every call starts with a fresh empty book.

Function

processOrders(commands: int[][]) → int[][]

Examples

Example 1

commands = [[0,1,1,100,4],[0,2,1,100,6],[0,3,0,100,7],[1,2,0,0,0],[0,4,0,100,1]]return = [[3,1,100,4],[3,2,100,3]]

Sellers 1 and 2 both ask 100, so seller 1 has priority because it arrived first. Buyer 3 takes all 4 units from seller 1, then 3 units from seller 2, both at the resting price 100.

Seller 2 has 3 units left. Its CANCEL removes those units. Buyer 4 then finds no sell order and waits in the book, producing no further trade.

Example 2

commands = [[0,1,0,105,2],[0,2,0,110,3],[0,3,1,100,4]]return = [[2,3,110,3],[1,3,105,1]]

Seller 3 can trade with both bids. Buyer 2's price 110 has priority over buyer 1's price 105, even though buyer 2 arrived later.

Seller 3 fills 3 units with buyer 2 at 110, then its last unit with buyer 1 at 105. These are the two resting buy prices, rather than the incoming sell limit 100. Buyer 1 keeps 1 unfilled unit.

Example 3

commands = [[0,1,0,99,2],[0,2,1,100,2],[1,99,0,0,0],[0,3,0,100,1]]return = [[3,2,100,1]]

The buy limit 99 is below the ask 100, so the first two orders do not trade. Cancelling unknown ID 99 has no effect.

Buyer 3's limit equals the ask. It takes 1 unit from seller 2 at 100. Seller 2 keeps 1 unit, and buyer 1 still waits at 99.

Example 4

commands = [[0,1,1,100,5],[0,2,1,100,5],[2,1,0,100,5],[0,3,0,100,6]]return = [[3,2,100,5],[3,1,100,1]]

Sellers 1 and 2 both ask 100, and seller 1 arrived first. The MODIFY of order 1 keeps the same price and quantity, but the order still moves to the back of the queue. Buyer 3 therefore takes all 5 units from seller 2 first, then 1 unit from seller 1.

Example 5

commands = [[0,1,0,95,4],[0,2,1,105,3],[2,1,0,106,2],[2,2,0,110,1],[2,9,0,50,5]]return = [[1,2,105,2]]

Buyer 1 bids 95 and seller 2 asks 105, so nothing trades. Modifying buyer 1 to price 106 and quantity 2 makes it cross at once: it buys 2 units from seller 2 at the resting price 105, which leaves seller 2 with 1 unit. Buyer 1 is now fully filled and leaves the book. Modifying seller 2 to 110 changes only its price because no buyer is left. Order 9 was never added, so its MODIFY does nothing.

Constraints

  • 0 <= commands.length <= 1000.
  • Each command contains exactly 5 integers.
  • Every value in commands is between 0 and 10^6, inclusive.
  • Only the three command forms described in the statement are valid.
  • IDs on ADD commands never repeat within the batch.

More Akuna Capital problems

  • Delivery Management SystemOA · Seen Nov 2026
  • Count Server ReplacementsOA · Seen Nov 2026
  • Maximum Remaining Drone PowerOA · Seen Nov 2026
All Akuna Capital interview questionsSee Akuna Capital hiring insights
Saved
class Solution {
    public int[][] processOrders(int[][] commands) {
        // write your code here
    }
}
Checking account…