Order book 
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.
Source Code
Source code access is restricted. Log in to request access.
Challenge History
No challenges yet.
Run Statistics
| # | Date | Score | Wall Time | CPU User | CPU System | Memory | Error | |
|---|---|---|---|---|---|---|---|---|
| 1 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: No answer to stdout. Died or killed? | |
| 2 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8660337 but the index is 8660337 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 3 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: No answer to stdout. Died or killed? | |
| 4 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: No answer to stdout. Died or killed? | |
| 5 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: No answer to stdout. Died or killed? | |
| 6 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8454536 but the index is 8454536 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 7 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8556422 but the index is 8556422 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 8 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: No answer to stdout. Died or killed? | |
| 9 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: No answer to stdout. Died or killed? | |
| 10 | Feb 15, 2024 19:01 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8542684 but the index is 8542684 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 11 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8490107 but the index is 8490107 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 12 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8541206 but the index is 8541206 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 13 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: No answer to stdout. Died or killed? | |
| 14 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8644010 but the index is 8644010 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 15 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8635258 but the index is 8635258 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 16 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8581559 but the index is 8581559 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 17 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8486192 but the index is 8486192 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 18 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8584656 but the index is 8584656 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 19 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8601493 but the index is 8601493 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 20 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8619085 but the index is 8619085 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 21 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: No answer to stdout. Died or killed? | |
| 22 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8625625 but the index is 8625625 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 23 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8528315 but the index is 8528315 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 24 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8592253 but the index is 8592253 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |
| 25 | Feb 15, 2024 19:00 | 0 | 0 | 0 | 0 | 0 | Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8664153 but the index is 8664153 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | stderr |