Price-Time Priority Matching for a Decentralised Exchange Offer Book

Last updated: November 29, 2026

Quick Overview

Implement an order book for one currency pair that accepts buy and sell offers with integer prices and quantities, crosses incoming offers against the best resting offers with partial fills at the maker's price, rests any remainder, and supports cancellation.

Ripple
Coding & Algorithms
Software Engineer
Ripple
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement an order book for one currency pair that accepts buy and sell offers with integer prices and quantities, crosses incoming offers against the best resting offers with partial fills at the maker's price, rests any remainder, and supports cancellation.

The XRP Ledger has a built-in decentralised exchange where offers cross automatically, and Ripple's liquidity and prime brokerage work relies on understanding order books. The interviewer checks heap usage, careful partial-fill bookkeeping, and an efficient cancellation strategy.

What the Interviewer Expects
  • Use a max-heap for bids and a min-heap for asks with a sequence tiebreaker for time priority
  • Execute at the resting (maker) offer's price
  • Handle partial fills on both sides
  • Implement cancellation with lazy deletion instead of O(n) heap removal
  • Return a clear fill record
Key Topics to Cover
Heaps
Order Book
Price-Time Priority
Lazy Deletion
Matching Engine
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 an immediate-or-cancel or fill-or-kill flag?
  • How would you add an auto-bridged path so a USD to MXN taker can also cross USD to XRP and XRP to MXN books?
  • Why must matching be deterministic on every validator?
  • How would you expose top-of-book depth efficiently?
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

place(id, side, price, qty) returns a list of fills (taker_id, maker_id, price, qty) and rests any remainder. A buy at price p crosses any ask at or below p, best price first, earliest first withi...

High-Level Design

Two heaps: bids keyed by (-price, seq) and asks by (price, seq). The monotonically increasing seq gives time priority. A live dictionary maps id to the mutable offer; cancel just deletes from ...


Submit Your Answer
Markdown supported

Related Questions