Order Book with Cancel by Order ID
Updated
Problem statement
Implement an order book for one stock. It keeps the resting buy and sell orders, totals their quantity by price, and can cancel any single order by its id. The book does no matching. Process operations on a book that starts empty.
Each order has an orderId, a side (0 for a buy order, 1 for a sell order), a price and a quantity. Each operation is one of:
[0, orderId, side, price, quantity]: add. The order rests in the book. Orders are never matched, so a buy order priced at or above a sell order also just rests. Nothing is recorded.[1, orderId]: cancel. Remove that order from the book. Every other order keeps its quantity, including orders at the same price. If no resting order has this id (it was never added, or it was already cancelled), nothing happens. Nothing is recorded.[2, orderId, quantity]: modify. The resting order's quantity becomesquantity. If no resting order has this id, nothing happens. Nothing is recorded.[3, side]: best price. Record[price, total]for the best price on that side, which is the highest buy price or the lowest sell price.totalis the sum of the quantities of all resting orders on that side at that price. If that side has no resting orders, record an empty list[].[4, side, price]: depth. Record[total], the sum of the quantities of the resting orders on that side atprice, or[0]if there are none.
Return the recorded lists in operation order.
Function
orderBook(operations: int[][]) → int[][]Examples
Example 1
operations = [[0,1,0,100,5],[0,2,0,100,3],[0,3,1,105,4],[0,4,0,99,10],[3,0],[1,1],[3,0],[4,0,100],[3,1]]return = [[100,8],[100,3],[3],[105,4]]Buy orders 1 and 2 both rest at price 100, so that level holds 5 + 3 = 8 and is the highest buy price: record [100, 8]. Cancelling order 1 removes only its 5. Order 2 still rests at 100, so the best buy is now [100, 3] and the depth at 100 is [3]. The only sell order is order 3, so the best sell is [105, 4].
Example 2
operations = [[0,7,1,50,2],[0,8,1,52,6],[0,9,0,55,1],[1,7],[1,7],[3,1],[2,8,4],[4,1,52],[3,0],[1,9],[3,0]]return = [[52,6],[4],[55,1],[]]Sell orders 7 (price 50) and 8 (price 52) rest. Buy order 9 at 55 is above both, but nothing is matched, so it rests too. Cancelling order 7 empties the sell level at 50. The second cancel of order 7 does nothing. The best sell is now [52, 6]. Order 8 is modified to quantity 4, so the depth at sell price 52 is [4]. The best buy is [55, 1], and after order 9 is cancelled the buy side is empty, so the last query records [].
Example 3
operations = [[0,1,0,10,3],[4,1,10],[1,42],[2,42,5],[3,1],[4,0,10]]return = [[0],[],[3]]Order 1 is a buy at 10, so the sell side has nothing at 10: record [0]. No order has id 42, so its cancel and modify do nothing. The sell side is empty, so the best sell is [], and the buy side still holds [3] at price 10.
Constraints
1 <= operations.length <= 10^4- Each operation has one of the five forms above, and
sideis0or1. 1 <= orderId, price <= 10^91 <= quantity <= 10^4- No two add operations use the same
orderId.
More Citadel Phone Screen questions
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Currency Arbitrage CyclePHONE SCREEN · Seen Sep 2026
- Smart Queue of Latest PricesPHONE SCREEN · Seen Sep 2026
- Merge Price-Delta FeedsPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
- Top Ten Trades by Notional ValuePHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Paged KV Cache AccessNVIDIA · PHONE SCREENNVIDIA interview questions
- Balanced Session ManagerPalantir · PHONE SCREENPalantir interview questions
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- In-Memory Priority Task SchedulerRippling · PHONE SCREENRippling interview questions
- Design a Food Rating SystemApple · PHONE SCREENApple interview questions
- Key-Value Store on a Block DeviceLambda · PHONE SCREEN
- Mutable Stock Price Top KBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Rank-Based Cache with a Data SourceLinkedIn · PHONE SCREENLinkedIn interview questions
- LLM Provider Pool FailoverPerplexity AI · PHONE SCREENPerplexity AI interview questions
- ID Allocator with Assign and ReleaseSnap Inc. · PHONE SCREENSnap Inc. interview questions
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences