I was asked to implement a buy/sell order matcher and write tests for it. The detail that mattered was that a smaller order ID could belong to a later order. The prompt emphasized arrival time, while the supplied heap example used price as the first priority and timestamp to break ties.
Interview Details
Coding — Build a Buy/Sell Order Matcher
Each order had five fields: an ID, a side (buy or sell), a quantity, a price, and a timestamp. A buy could match a sell when the buyer’s price was at least the seller’s asking price. The ID identified the order, but its numerical value said nothing about when the order arrived.
The supplied example accumulated orders before running the matcher. It selected the highest bid and lowest ask first, then chose the earlier timestamp among orders at the same price. All quantities in that example were equal. The rewritten reference below also handles unequal quantities by leaving the unfilled balance on the book.
from dataclasses import dataclass
from heapq import heappop, heappush
from itertools import count
@dataclass
class Order:
order_id: int
side: str
quantity: int
price: int
timestamp: int
class OrderMatcher:
def
from dataclasses import dataclass
from heapq import heappop, heappush
from itertools import count
@dataclass
class Order:
order_id: int
side: str
quantity: int
price: int
timestamp: int
class OrderMatcher:
def
Unlock Full Details
Create a free account to unlock this interview experience report.