Code RoomOrder matching engine
HardPrep Room Coding #4875

Order matching engine

CodingAlgorithms & data structuresSenior–Staff~40 min

A matching engine keeps resting orders on two sides. Each entry of orders is 'B 105 4' or 'S 99 6': the side, a price in cents, and a quantity of at least 1. An arriving buy trades against the cheapest resting sell priced at or below it, and an arriving sell trades against the dearest resting buy priced at or above it, repeating while a trade is possible. Orders resting at the same price are taken oldest first. Each trade happens at the resting order's price and moves the smaller of the two open quantities. Whatever is left of the arriver rests on its own side. Return, for each arriving order in turn, the total cash in cents that its trades moved.

Implement
order_book_fills(orders: list[str]) → list[int]
Examples
in[["B 100 5","S 98 3","S 101 4"]]out[0,300,0]
in[["S 20 1","S 22 1","S 21 1","B 25 3"]]out[0,0,0,63]
in[["B 30 10","S 30 4","S 30 4","S 30 4"]]out[0,120,120,60]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 40 min
InputExpectedGot
[["B 100 5","S 98 3","S 101 4"]][0,300,0]not run yetsample
[["S 20 1","S 22 1","S 21 1","B 25 3"]][0,0,0,63]not run yetsample
[["B 30 10","S 30 4","S 30 4","S 30 4"]][0,120,120,60]not run yetsample