Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Order Book Transaction Pairing
00:00
5 left

Order Book Transaction Pairing

HardPython

Problem

Implement a simplified Voleon order book matcher. Orders arrive in the given sequence and are either limit or market orders, creating four combinations: buy limit, sell limit, buy market, and sell market.

Each order has a unique ID, side, type, and quantity. A limit order also has a price. Match every incoming order against resting orders on the opposite side using price-time priority. A buy limit can trade with the lowest-priced sell limit whose price is at most the buy price. A sell limit can trade with the highest-priced buy limit whose price is at least the sell price. Market orders trade at the best available opposite-side limit price. Market orders that cannot be fully executed are canceled and never rest in the book. A trade executes at the resting order's price.

Formal Specification

Write match_orders(orders). orders is a list of dictionaries in arrival order:

  • id: unique string
  • side: either buy or sell
  • type: either limit or market
  • price: integer for limit orders, and null for market orders
  • quantity: positive integer

Return a list of arrays in execution order. Each array is [buy_id, sell_id, quantity, price]. Partial fills must be emitted as separate trades when an incoming order matches multiple resting orders.

Constraints

  • 1 <= len(orders) <= 100000
  • 1 <= orders[i].quantity <= 10^9
  • 1 <= orders[i].price <= 10^9 for limit orders
  • Market orders have price set to null
  • Order IDs are unique strings
  • The input order sequence defines time priority

Function Signature

def match_orders(orders):
Interviewer

Your question is Order Book Transaction Pairing. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.