KatInterview

Cross-Venue Quote Book and Arbitrage Scanner

Data Structure DesignModel answer
MediumModel answerData Structure Designclass-designhash-mapmarket-data

Last updated

Build an in-memory engine that keeps the latest valid top-of-book quote per venue, expires old ones, and reports the most profitable buy-low/sell-high pair across venues.

Context

The same stock can be quoted on several trading venues at once. Each venue publishes its top of book: the best bid (what buyers will pay) and the best ask (what sellers want), each with a size. If one venue's bid is strictly higher than a different venue's ask for the same instrument, you can buy on the cheap venue and sell on the expensive one for a risk-free gain.

Task

Implement an ArbitrageEngine that consumes a stream of quotes one at a time and, after each one, reports the best immediately available cross-venue opportunity, if there is one.

from dataclasses import dataclass
from typing import Optional

@dataclass
class Quote:
    venue: str
    instrument: str
    bid_price: float
    bid_size: int
    ask_price: float
    ask_size: int
    timestamp_ms: int

@dataclass
class ArbitrageOpportunity:
    instrument: str
    buy_venue: str
    sell_venue: str
    buy_price: float
    sell_price: float
    quantity: int
    profit: float

class ArbitrageEngine:
    def __init__(self, quote_ttl_ms: int): ...
    def process_quote(self, quote: Quote) -> Optional[ArbitrageOpportunity]: ...

process_quote must:

  1. Validate the quote. A quote whose own bid is higher than its own ask is a corrupted feed and is rejected.
  2. Store it as that venue's latest quote for that instrument.
  3. Expire quotes that are too old, according to quote_ttl_ms.
  4. Return the opportunity with the highest total profit among the remaining fresh quotes, or None.

Rules

  • Prices are positive and have two decimal places.
  • Sizes are non-negative integers. A size of 0 means that side is inactive and cannot trade.
  • Timestamps never decrease for a given venue, but different venues may interleave slightly. The engine's current time is the largest timestamp it has accepted.
  • A quote is stale when current_time - quote.timestamp_ms > quote_ttl_ms.
  • Buying and selling on the same venue is not allowed.
  • Quantity is min(buy venue ask size, sell venue bid size), and profit is (sell price - buy price) * quantity.
  • Among candidate pairs, choose the largest total profit. If profits tie, choose the larger quantity.

Examples

engine = ArbitrageEngine(quote_ttl_ms=500)

engine.process_quote(Quote("EXCH_A", "AAPL", 150.10, 100, 150.20, 50, 1000))
# -> None (only one venue)

engine.process_quote(Quote("EXCH_B", "AAPL", 149.90, 80, 150.05, 40, 1050))
# -> ArbitrageOpportunity("AAPL", buy_venue="EXCH_B", sell_venue="EXCH_A",
#                         buy_price=150.05, sell_price=150.10,
#                         quantity=40, profit=2.00)

engine.process_quote(Quote("EXCH_C", "AAPL", 151.00, 50, 149.00, 50, 1100))
# -> None (this venue's own bid is above its own ask, so it is rejected)

engine.process_quote(Quote("EXCH_B", "AAPL", 149.00, 10, 150.00, 10, 1600))
# -> None (EXCH_A's quote is 600 ms old, beyond the 500 ms TTL)

What to cover

Explain your data model, the time and staleness conventions, how you validate input, how you compare prices safely, and the complexity. Then give the code.

Hints

Hint 1

Group state by instrument, then by venue, so each (instrument, venue) holds only the newest valid quote. Everything else can be derived from that.

Hint 2

Decide the rules up front: what counts as the current time, whether an age exactly equal to the TTL is stale, and whether a rejected quote may change any state.

Hint 3

Do not just pair the best bid with the best ask. The most profitable pair depends on sizes too, so try every ordered pair of distinct venues. Compare prices as integer cents to avoid float noise like 150.10 - 150.05.