FastPrepMarket Order Book
Problems/Market Order Book

Market Order Book

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Headlands Technologies phone screen. The report names a C++ market order book task. It does not say what operations are needed, how orders match, what the output is, or the limits. Here you add orders, cancel orders, and ask for the best price level with its total quantity. There is no trade matching. Our version is about 60% the same as the reported question.

Implement a finite in-memory order book for one instrument. Process the operations in operations from left to right.

  • ADD id BUY price quantity adds a new active buy order.
  • ADD id SELL price quantity adds a new active sell order.
  • CANCEL id removes that active order completely.
  • QUERY records the current best bid and best ask.

The best bid is the highest active buy price. The best ask is the lowest active sell price. For each best price, add the quantities of all active orders at that price.

For every QUERY, append one output string in the exact format bestBidPrice bestBidQuantity bestAskPrice bestAskQuantity. If one side is empty, use price -1 and quantity 0 for that side.

This book does not match or execute orders. An add remains active until it is canceled.

Function

processMarketOrderBook(operations: String[]) → String[]

Examples

Example 1

operations = ["ADD b1 BUY 100 5","ADD b2 BUY 100 3","ADD s1 SELL 104 7","QUERY","CANCEL b1","QUERY"]return = ["100 8 104 7","100 3 104 7"]

The first query aggregates both buy orders at price 100. After canceling b1, only quantity 3 remains at the best bid.

Example 2

operations = ["QUERY","ADD s1 SELL 105 4","ADD s2 SELL 103 2","ADD b1 BUY 99 6","QUERY"]return = ["-1 0 -1 0","99 6 103 2"]

The empty book uses the required sentinels. After the adds, price 103 is a better ask than 105.

Example 3

operations = ["ADD x BUY 10 2","ADD y BUY 12 5","QUERY","CANCEL y","QUERY"]return = ["12 5 -1 0","10 2 -1 0"]

Canceling the only order at price 12 exposes price 10 as the next best bid.

Constraints

  • 1 <= operations.length <= 100000.
  • Each operation has exactly one of the four documented forms.
  • Order IDs are unique among all ADD operations and contain only ASCII letters and digits.
  • Every CANCEL names an active order.
  • 1 <= price, quantity <= 10^9.
  • Every aggregated quantity fits in a signed 64-bit integer.

More Headlands Technologies Phone Screen questions

  • Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
  • Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
  • Pearson Correlation CoefficientPHONE SCREEN · Seen Apr 2026
  • Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
  • Queue Mean from Corrective SnapshotsPHONE SCREEN · Seen Mar 2026
  • Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
  • CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
  • Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
  • Top Trade Events by NotionalPHONE SCREEN · Seen May 2024
  • Fixed-Size Memory PoolPHONE SCREEN · Seen May 2024

Similar questions at other companies

  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
  • Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
  • Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
  • Build a Pivot Table with TotalsSigma Computing · PHONE SCREEN
  • Structured Log Stream with QueriesOpenAI · PHONE SCREENOpenAI interview questions
  • Banking System with Pending Transfer AcceptanceCircle · PHONE SCREENCircle interview questions
  • Recent Metrics by Name and TagsVercel · PHONE SCREEN
  • Streaming Top-K Frequent ElementsNVIDIA · PHONE SCREENNVIDIA interview questions
  • Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
  • Interview Slot BookingTrueFoundry · PHONE SCREEN
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public String[] processMarketOrderBook(String[] operations) {
        // write your code here
    }
}
Checking account…