Maintain Top of Book From an Order Add Cancel Modify Feed

Last updated: November 29, 2026

Quick Overview

Process a stream of add, cancel and modify messages for a single instrument's limit order book and support a query that returns the best bid and best ask with their aggregate size at any time.

Balyasny
Coding & Algorithms
Software Engineer
Balyasny
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Process a stream of add, cancel and modify messages for a single instrument's limit order book and support a query that returns the best bid and best ask with their aggregate size at any time.

Execution Technology at Balyasny builds order routing and execution algorithms that consume exchange depth feeds, so reconstructing a book from order-level messages is a core skill. The interviewer checks data structure choice, correctness on cancels of the best level, and awareness of latency trade-offs.

What the Interviewer Expects
  • Keep an order-id map so cancels and modifies are O(1) lookups
  • Aggregate quantity per price level per side
  • Use heaps or sorted structures for best price with a strategy for removing empty levels
  • Handle a cancel or modify that empties the current best level
  • Discuss amortized cost of lazy deletion versus a balanced tree
Key Topics to Cover
order book
heaps with lazy deletion
hash maps
market data
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return the top 5 levels of depth instead of just the best?
  • Exchanges send executions that reduce resting size; how do they fit this model?
  • How would you change the design in C++ for a feed doing millions of messages per second?
  • How do you detect and recover from a gap in feed sequence numbers?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Messages: add(id, side, price, qty), cancel(id), modify(id, new_qty) (size change at the same price; a price change is cancel plus add). Query bbo() returns `((bid_px, bid_qty), (ask_px, ask_q...

High-Level Design

Three structures: orders maps id to (side, price, qty); level[side] maps price to aggregate quantity; heap[side] holds prices (bids negated) to find the best. When a level drops to zero we delet...


Submit Your Answer
Markdown supported

Related Questions