Match Limit Orders in a Price-Time Priority Order Book

Last updated: November 29, 2026

Quick Overview

Implement an order book that accepts limit orders (id, side, price, quantity), matches each incoming order against resting orders on the other side by best price then earliest arrival, returns the fills, and rests any unfilled remainder.

J.P. Morgan
Coding & Algorithms
Software Engineer
J.P. Morgan
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement an order book that accepts limit orders (id, side, price, quantity), matches each incoming order against resting orders on the other side by best price then earliest arrival, returns the fills, and rests any unfilled remainder.

Electronic trading is central to J.P. Morgan's Markets business, and a simplified matching engine is a common onsite coding problem for trading-technology candidates. The interviewer checks that you choose heaps with the right keys, preserve time priority on partial fills, and execute at the resting order's price.

What the Interviewer Expects
  • Use a max-heap for bids and a min-heap for asks
  • Break price ties by arrival sequence to get time priority
  • Execute fills at the resting order's price, not the incoming one
  • Keep a partially filled resting order's original sequence number
  • Explain O(log n) per resting order touched
Key Topics to Cover
heaps
priority queues
simulation
order books
price-time priority
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 support cancel by order id efficiently?
  • Why might a real engine use a sorted map of price levels with FIFO queues instead of one heap?
  • How would you add market orders and immediate-or-cancel orders?
  • How would you make the book deterministic and replayable for audit?
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

submit(order_id, side, price, qty) returns a list of (buy_id, sell_id, price, qty) fills. A buy crosses any ask priced at or below its limit; a sell crosses any bid at or above its limit. Better p...

High-Level Design

Two heaps. Asks are a min-heap of (price, seq, id, qty). Bids are stored as (-price, seq, id, qty) so Python's min-heap gives the highest bid. The monotonically increasing seq makes ties resolve...


Submit Your Answer
Markdown supported

Related Questions