Quant Memo
Coding/●●●●●

Design a stop-order trigger engine

Asked at IMC, DRW

Design an engine holding resting stop orders:

  • add_stop(side, stop_price), register a stop. A buy stop fires when the last trade price rises to stop_price or above; a sell stop fires when the last price falls to stop_price or below. Return an order id.
  • cancel(order_id), remove a resting stop.
  • on_price(last), feed a new last-trade price; return the list of stops that fire now (each fires at most once).
b = eng.add_stop("buy", 105)
s = eng.add_stop("sell", 95)
eng.on_price(100)   -> []              # inside the band, nothing fires
eng.on_price(106)   -> [("buy", 105)]  # price crossed up through 105

Every operation should be O(logn)O(\log n) amortized.

Show a hint

As the price rises, buy stops fire from the lowest price upward; as it falls, sell stops fire from the highest price downward. That is a min-heap of buy stops and a max-heap of sell stops. Cancels are the same trouble as in an order book: you cannot delete from the middle of a heap.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions