Quant Memo
Coding/●●●●

Merge K sorted trade logs with a heap

Asked at Jump Trading, Citadel

Each of your kk exchange gateways writes a log already sorted by timestamp. You need one globally sorted stream for replay.

logs = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
merge_sorted(logs)  ->  [1, 2, 3, 4, 5, 6, 7, 8, 9]

Merge the kk sorted lists into one sorted list. Do better than concatenating and re-sorting. What is the cost?

Show a hint

At any moment the next output value is the smallest among the current fronts of the kk lists, only kk candidates, not NN. What structure hands you the minimum of kk things and lets you replace it cheaply?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions