Number of Orders in the Backlog - Problem

You are given a 2D integer array orders, where each orders[i] = [pricei, amounti, orderTypei] denotes that amounti orders have been placed of type orderTypei at the price pricei. The orderTypei is:

  • 0 if it is a batch of buy orders, or
  • 1 if it is a batch of sell orders.

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

  • If the order is a buy order, you 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 current buy order's price, they will match and be executed, and that sell order will be removed from the backlog. Else, the buy order is added to the backlog.
  • Vice versa, if the order is a sell order, you 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 current sell order's price, they will match and be executed, and that buy order will be removed from the backlog. Else, the sell order is added to the backlog.

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

Input & Output

Example 1 — Basic Matching
$ Input: orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
Output: 6
💡 Note: Buy 10 (5 units) matches with sell 15 (2 units), leaving buy 10 (3 units). Sell 25 (1 unit) matches with buy 30 (4 units), leaving buy 30 (3 units). Total backlog: 3 + 3 = 6
Example 2 — No Matches
$ Input: orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
Output: 999999984
💡 Note: Sell 7 can't match with any buy order, buy orders can't match with remaining sell. Only some units of large sell order get matched.
Example 3 — Complete Match
$ Input: orders = [[10,2,0],[8,3,1]]
Output: 1
💡 Note: Buy 10 (2 units) matches with sell 8 (3 units) since 8 ≤ 10. 2 units matched, leaving sell 8 (1 unit) in backlog.

Constraints

  • 1 ≤ orders.length ≤ 105
  • orders[i].length == 3
  • 1 ≤ pricei, amounti ≤ 109
  • orderTypei is either 0 or 1

Visualization

Tap to expand
Number of Orders in the Backlog INPUT orders = [[price, amount, type]] [10, 5, 0] BUY price=10, amt=5 [15, 2, 1] SELL price=15, amt=2 [25, 1, 1] SELL price=25, amt=1 [30, 4, 0] BUY price=30, amt=4 Data Structures: Buy Heap (Max Heap) Sell Heap (Min Heap) Process orders sequentially ALGORITHM STEPS 1 Process BUY [10,5] Sell heap empty Add to Buy heap: (10,5) 2 Process SELL [15,2] Max buy=10 < 15 Add to Sell heap: (15,2) 3 Process SELL [25,1] Max buy=10 < 25 Add to Sell heap: (25,1) 4 Process BUY [30,4] Min sell=15 <= 30: Match! Consume (15,2): left=2 Min sell=25 <= 30: Match! Consume (25,1): left=1 Add to Buy heap: (30,1) Final Backlogs: Buy Heap (10,5)(30,1) Sell Heap (empty) Sum amounts: 5 + 1 = 6 FINAL RESULT Backlog Summary Buy Orders in Backlog price=10 amt=5 price=30 amt=1 Sell Orders in Backlog (All matched - empty) Calculation 5 + 1 + 0 = 6 mod (10^9 + 7) = 6 OUTPUT 6 OK - Verified Key Insight: Use two heaps: Max-Heap for buy orders (highest price first) and Min-Heap for sell orders (lowest price first). When matching orders: buy price >= sell price triggers execution. Process greedily to maximize matches. Time: O(n log n) for heap operations. Space: O(n) for storing unmatched orders in heaps. TutorialsPoint - Number of Orders in the Backlog | Optimal Solution (Two Heaps)
Asked in
Amazon 35 Microsoft 28 Google 22 Apple 18
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen