Skip to content
BytePatterns

Bid-Ask Spread After Each Quote

EasyTwo Heaps & K-Way Merge#two-heaps#max-heap#min-heap~15m

Problem

A quote board receives quotes as ("buy", price) or ("sell", price). Nothing trades on this board, so every quote stays. After each quote, report the spread: the lowest sell price minus the highest buy price. When either side has no quotes yet, report None. Return the list of reports.

Examples

Input:  quotes = [("buy", 100), ("sell", 105), ("buy", 102), ("sell", 104), ("buy", 99)]
Output: [None, 5, 3, 2, 2]
Why:    the best buy climbs to 102 and the best sell drops to 104, and the low buy at 99 changes nothing
Input:  quotes = [("sell", 50), ("sell", 40)]
Output: [None, None]
Why:    nobody has quoted a buy price yet
Input:  quotes = [("buy", 12), ("sell", 10)]
Output: [None, -2]
Why:    edge case, nothing trades here, so a crossed board simply shows a negative spread

Hints

0 / 3

Stuck on the idea rather than the code? Heap Basics covers it.