Order book Yuriy Lyfenko

Simulate a sell-side order book and calculate the cost of a final purchase as fast as possible.

Input

1,000,000 order updates on STDIN, one per line:

  • + <price> <size> – add a new sell order
  • - <position> – delete the order at the given position
  • = <size> – buy <size> shares from the top of the order book

After all updates are processed, buy 1,000 shares from the top of the order book.

Output

Print the total cost of the final 1,000-share purchase to STDOUT.

Order Book Rules

Orders are sorted by price ascending (lower is better). Orders at the same price are sorted by arrival time (earlier first). Position 0 is the best (lowest-price) offer.

The = (buy) operation consumes shares starting from position 0. If an order is fully consumed, it is removed from the book.

Example

Input Order book state
+ 1137 100 (1137,100)
+ 1130 10 (1130,10), (1137,100)
+ 1130 50 (1130,10), (1130,50), (1137,100)
- 0 (1130,50), (1137,100)
+ 1150 200 (1130,50), (1137,100), (1150,200)
= 200 (1150,150)

Total cost of the last buy: 50 * 1130 + 100 * 1137 + 50 * 1150.

Back to listJan 13, 2024 20:34yenw0dyenw0dError
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 13, 2024 20:3300000Error: Exit with code 2: panic: price level index out of bounds: 4114 goroutine 1 [running]: main.(*ArrayBookSide).Insert(0xc000d0e000?, 0xfd?, 0x555a944c8a93?) /tmp/solution1087200636/arraybacked.go:36 +0x49a main.OrderBookMain({0xc000d0e000, 0x834f...stderr
2Jan 13, 2024 20:3300000Error: Exit with code 2: panic: price level index out of bounds: 4097 goroutine 1 [running]: main.(*ArrayBookSide).Insert(0xc000d0e000?, 0x45?, 0x55a7ba61ba93?) /tmp/solution2019961051/arraybacked.go:36 +0x49a main.OrderBookMain({0xc000d0e000, 0x82ab...stderr
3Jan 13, 2024 20:3300000Error: Exit with code 2: panic: price level index out of bounds: 4109 goroutine 1 [running]: main.(*ArrayBookSide).Insert(0xc000d0e000?, 0x62?, 0x9c?) /tmp/solution1306527666/arraybacked.go:36 +0x49a main.OrderBookMain({0xc000d0e000, 0x83ef3b, 0xa120...stderr
4Jan 13, 2024 20:34150,308168,323,63987,178,70102,260,992
5Jan 13, 2024 20:34150,517149,533,56387,300,08002,285,568
6Jan 13, 2024 20:34151,077152,382,74887,624,78902,256,896
7Jan 13, 2024 20:34174,272123,833,25289,069,00012,009,0002,433,024
8Jan 13, 2024 20:34180,052127,638,05296,397,0008,033,0002,310,144
9Jan 13, 2024 20:34180,447130,299,49696,531,0008,128,0002,531,328