KatInterview

Top-of-Book Quotes per Venue and Across All Venues

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designhash-mapaggregationorder-book

Last updated

Aggregate order records by price level to answer per-venue and market-wide best bid and ask queries, summing the size at the best price.

You are given a fixed collection of resting orders from several trading venues, followed by a batch of queries. Your job is to answer each query with the best buy price and best sell price, along with the total size available at each of those prices.

Input

  • orders: a list of order records. Each record is a list [venue_id, price, quantity, side]:
    • venue_id: a string or an integer naming the venue.
    • price: a positive number.
    • quantity: a positive integer.
    • side: "B" for a buy order (bid) or "A" for a sell order (ask).
  • queries: a list of query names, each either "get_exchange_bbo" or "get_nbbo".
  • query_args: a list with the same length as queries. Entry i holds the arguments for queries[i]:
    • for "get_exchange_bbo", a one-element list [venue_id];
    • for "get_nbbo", an empty list [].

The orders never change between queries.

Definitions

  • The best bid in a scope is the highest price among bid orders in that scope.
  • The best ask in a scope is the lowest price among ask orders in that scope.
  • The size of a best quote is the sum of the quantities of all orders on that side that sit exactly at that best price within the scope.
  • For "get_exchange_bbo", the scope is only the orders of the given venue.
  • For "get_nbbo", the scope is the orders of all venues combined. If several venues quote the same best price, their quantities are added together.

Output

Return a list with one result per query, in the same order as queries. Each result is a two-element list [best_bid, best_ask], where each side is either:

  • [price, total_quantity] (a list, not a tuple), or
  • None if that side has no orders in the scope.

Further rules:

  • A venue that does not appear in orders yields [None, None] (no error).
  • If a query name is not one of the two supported names, raise ValueError.
  • Do not adjust or "uncross" the data: if a best bid is at or above a best ask, report both as they are.
  • Prices are compared by value as dictionary keys would be, so 100 and 100.0 count as the same price level. Venue ids "1" and 1 are different venues.

Constraints

  • The number of orders can be large (on the order of 10^5 or more), so the batch of queries should be answered efficiently rather than by rescanning all orders each time.
  • price > 0, quantity > 0.
  • side is always "B" or "A".
  • len(queries) == len(query_args).
  • orders may be empty.

Examples

Example 1

Input: top_of_book_queries([[1, 100, 10, 'B'], [1, 101, 5, 'A'], [2, 100, 7, 'B'], [2, 99, 4, 'A']], ['get_exchange_bbo', 'get_exchange_bbo', 'get_nbbo'], [[1], [2], []])

Output: [[[100, 10], [101, 5]], [[100, 7], [99, 4]], [[100, 17], [99, 4]]]

Hints

Hint 1

Because the orders never change, you can group them once up front instead of rescanning for every query.

Hint 2

For each scope (every venue, plus one combined scope) and each side, keep a map from price to total quantity at that price.

Hint 3

The best bid is the maximum key of the bid map and the best ask is the minimum key of the ask map; the value stored under that key is already the summed size. Build the combined scope in the same pass so ties across venues are summed correctly.