Count the Orders Left in a Buy and Sell Backlog After Price Matching

Quick Overview

Simulate a simple order book where batches of buy and sell orders arrive in sequence and match against the best opposing price, then count the orders left unmatched modulo 10^9 + 7. It tests priority queue use, greedy matching of partial batches, and handling of very large amounts.

Count the Orders Left in a Buy and Sell Backlog After Price Matching

Company: Robinhood

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list `orders`, where each `orders[i] = [price_i, amount_i, order_type_i]` means that `amount_i` orders of type `order_type_i` have been placed at the price `price_i`. The order type is `0` for a batch of buy orders and `1` for a batch of sell orders. Each `orders[i]` is a batch of `amount_i` independent orders with the same price and order type. All orders in `orders[i]` are placed before all orders in `orders[i+1]`. There is a backlog of orders that have not been executed. The backlog starts empty. When an order is placed, the following happens: - If the order is a buy order, look at the sell order with the smallest price in the backlog. If that sell order's price is smaller than or equal to the buy order's price, they match and are executed, and that sell order is removed from the backlog. Otherwise the buy order is added to the backlog. - If the order is a sell order, look at the buy order with the largest price in the backlog. If that buy order's price is larger than or equal to the sell order's price, they match and are executed, and that buy order is removed from the backlog. Otherwise the sell order is added to the backlog. Return the total number of orders in the backlog after all the orders from the input have been placed. Since this number can be large, return it modulo `10^9 + 7`. ### Function Signature ```python def get_number_of_backlog_orders(orders: list[list[int]]) -> int: ``` ### Rules - Each row of `orders` is `[price, amount, order_type]`, with `order_type` equal to `0` for buy and `1` for sell. - Rows are processed in input order. The `amount` orders of one row are placed one at a time, so a single row can match several orders already in the backlog and still leave a remainder. - Equal prices match: a buy at price `p` matches a sell at price `p`. - The answer is the number of individual orders left on both sides (the sum of the remaining amounts), taken modulo `10^9 + 7`. ### Constraints - `1 <= len(orders) <= 10^5` - `len(orders[i]) == 3` - `1 <= price_i, amount_i <= 10^9` - `order_type_i` is either `0` or `1` ### Examples ```text Input: orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]] Output: 6 ``` The 5 buys at 10 go to the backlog. The sells at 15 and 25 cannot match because the highest buy price (10) is below both, so they go to the backlog too. The 4 buys at 30 match the 2 sells at 15 and the 1 sell at 25, and the last buy at 30 stays. The backlog ends with 5 buys at 10 and 1 buy at 30, which is 6 orders. ```text Input: orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]] Output: 999999984 ``` The 3 buys at 15 match 3 of the sells at 7, leaving 999999997 sells. The buys at 5 cannot match a sell at 7, so all 999999995 go to the backlog. The single sell at 5 matches one buy at 5, leaving 999999994 buys. The total is 999999994 + 999999997 = 1999999991, and 1999999991 modulo 1000000007 is 999999984.

Overview: Simulate a simple order book where batches of buy and sell orders arrive in sequence and match against the best opposing price, then count the orders left unmatched modulo 10^9 + 7. It tests priority queue use, greedy matching of partial batches, and handling of very large amounts.

Robinhood logo
Robinhood
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list orders, where each orders[i] = [price_i, amount_i, order_type_i] means that amount_i orders of type order_type_i have been placed at the price price_i. The order type is 0 for a batch of buy orders and 1 for a batch of sell orders.

Each orders[i] is a batch of amount_i independent orders with the same price and order type. All orders in orders[i] are placed before all orders in orders[i+1].

There is a backlog of orders that have not been executed. The backlog starts empty. When an order is placed, the following happens:

  • If the order is a buy order, look at the sell order with the smallest price in the backlog. If that sell order's price is smaller than or equal to the buy order's price, they match and are executed, and that sell order is removed from the backlog. Otherwise the buy order is added to the backlog.
  • If the order is a sell order, look at the buy order with the largest price in the backlog. If that buy order's price is larger than or equal to the sell order's price, they match and are executed, and that buy order is removed from the backlog. Otherwise the sell order is added to the backlog.

Return the total number of orders in the backlog after all the orders from the input have been placed. Since this number can be large, return it modulo 10^9 + 7.

Function Signature

def get_number_of_backlog_orders(orders: list[list[int]]) -> int:

Rules

  • Each row of orders is [price, amount, order_type] , with order_type equal to 0 for buy and 1 for sell.
  • Rows are processed in input order. The amount orders of one row are placed one at a time, so a single row can match several orders already in the backlog and still leave a remainder.
  • Equal prices match: a buy at price p matches a sell at price p .
  • The answer is the number of individual orders left on both sides (the sum of the remaining amounts), taken modulo 10^9 + 7 .

Constraints

  • 1 <= len(orders) <= 10^5
  • len(orders[i]) == 3
  • 1 <= price_i, amount_i <= 10^9
  • order_type_i is either 0 or 1

Examples

Input: orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
Output: 6

The 5 buys at 10 go to the backlog. The sells at 15 and 25 cannot match because the highest buy price (10) is below both, so they go to the backlog too. The 4 buys at 30 match the 2 sells at 15 and the 1 sell at 25, and the last buy at 30 stays. The backlog ends with 5 buys at 10 and 1 buy at 30, which is 6 orders.

Input: orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
Output: 999999984

The 3 buys at 15 match 3 of the sells at 7, leaving 999999997 sells. The buys at 5 cannot match a sell at 7, so all 999999995 go to the backlog. The single sell at 5 matches one buy at 5, leaving 999999994 buys. The total is 999999994 + 999999997 = 1999999991, and 1999999991 modulo 1000000007 is 999999984.