Order Matching on Execute
Updated
Problem statement
Maintain buy and sell orders. Adding an order only stores it. Whenever an EXECUTE command occurs, repeatedly match the highest-priced buy with the lowest-priced sell while the buy price is at least the sell price.
For this exercise, assume equal-priced orders on either side use arrival order. Each match trades the smaller remaining quantity at the price of the order that arrived earlier. Decrease both quantities and remove an order only after its quantity reaches zero. Unfilled quantities remain for later executions. There is no automatic execution after the final command.
Return fills in execution order, each as buyId,sellId,quantity,price. The command grammar and finite bounds are listed below. Cancellation and order modification are outside this exercise.
Function
executeOrderBook(commands: String[][]) → String[]Examples
Example 1
commands = [["BUY","b1","100","5"],["SELL","s1","90","3"]]return = []Both orders are stored, but no EXECUTE command occurs. The result contains no fills.
Example 2
commands = [["BUY","b1","100","5"],["SELL","s1","90","3"],["EXECUTE"]]return = ["b1,s1,3,100"]The orders cross and trade 3 units. Buy b1 arrived first, so its price 100 is used. It retains 2 unfilled units.
Example 3
commands = [["SELL","s1","90","2"],["BUY","b1","100","1"],["BUY","b2","100","3"],["EXECUTE"]]return = ["b1,s1,1,90","b2,s1,1,90"]The equal-priced buys use arrival order. Buy b1 trades one unit first, then b2 trades the remaining sell unit. Sell s1 is older than both buys, so both trades use price 90.
Constraints
0 <= commands.length <= 500.- Every command has
1or4tokens; each token has length at most12. - A command is
[BUY,id,price,quantity],[SELL,id,price,quantity]or[EXECUTE]. Each order ID is unique and matches[a-z][a-z0-9]{0,11}. Price and quantity are decimal strings for integers in1..1000000.
More Squarepoint Capital interview questions
- Mutable Record Store with Error ResultsONSITE INTERVIEW · Seen Sep 2026
- Maximum SubarrayONSITE INTERVIEW · Seen Aug 2026
- Product of Array Except SelfONSITE INTERVIEW · Seen Aug 2026
- Estimate Pi with a Finite SeriesONSITE INTERVIEW · Seen Oct 2025
- In-Memory Directory and File OperationsONSITE INTERVIEW · Seen Oct 2025
- Count C++ Constructor and Assignment CallsONSITE INTERVIEW · Seen Jun 2025
- Observe a Lazy GeneratorONSITE INTERVIEW · Seen Feb 2025
- Accumulate Numeric Value ObjectsONSITE INTERVIEW · Seen Sep 2024
- Differentiate a PolynomialONSITE INTERVIEW · Seen Aug 2023
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Meeting Rooms III: Most Booked RoomGoogle · ONSITE INTERVIEWGoogle interview questions
- Insert into a Min-HeapGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Assign Partitions to the Least-Loaded ServersAmazon · ONSITE INTERVIEW
- Cursor-Based Pagination Over Sorted LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Issue and Reuse 16-Bit TokensMeta · ONSITE INTERVIEWMeta interview questions
- Weighted Server Load Balancer with TTLStripe · ONSITE INTERVIEWStripe interview questions
- Update Pod Counts From LogsSalesforce · OASalesforce interview questions
- Meeting Rooms IIIBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Shortest-Job-First Process SchedulingByteDance · PHONE SCREENByteDance interview questions
- Dynamic Batch Completion TimesAmbience Healthcare · PHONE SCREEN