Cross-Margin Liquidation Engine 
Simulate a cross-margin derivatives engine: process trades, track account equity, and liquidate undercollateralized accounts as fast as possible.
Accounts are funded with a USD deposit and trade instruments whose prices change over time. When a price update causes an account’s equity to fall below 1% of its total position notional, the account must be liquidated.
Definitions
- Account equity =
balance + Σ(size_i * price_i) - Σ(total_paid_i) - Total position notional =
Σ(|size_i| * price_i) - Total paid for instrument
i= signed sum oftrade_size * price_at_trade_timeacross all trades - Margin rule: liquidate if
equity < total_notional / 100after any price update
Input
One command per line on STDIN:
a <balance>– create account with USD balance (IDs start at0, incrementing)p <instrument_idx> <price>– set/update instrument price (0-indexed)t <account_idx> <instrument_idx> <size>– tradesize(signed) at current price- Final line:
<account_idx>– query this account and terminate
Output
On each price update, liquidate all accounts violating the margin rule. For each liquidation, print:
liquidate <account_id> <equity> <position_notional>
Liquidation order: largest total position notional first, then account ID descending as tie-breaker. Liquidated accounts have their balance and positions cleared.
For the final query, print <equity> <notional> for the requested account.
Constraints
- Accounts: <= 100,000
- Instruments: <= 1,000
- Price range: 100 to 1,000,000
- Trade size range: 1 to 10,000
Example
a 100
p 0 100
t 0 0 10
p 0 90
0
Output:
liquidate 0 0 900
0 0
Source code access is restricted. Log in to request access.
No challenges yet.
| # | Date | Score | Wall Time | CPU User | CPU System | Memory | Error | |
|---|---|---|---|---|---|---|---|---|
| 1 | Dec 30, 2025 04:38 | 2,992 | 66,191,936 | 35,901,000 | 2,991,000 | 2,408,448 | ||
| 2 | Dec 30, 2025 04:45 | 3,112 | 63,247,462 | 36,414,000 | 4,046,000 | 2,461,696 | ||
| 3 | Dec 30, 2025 04:38 | 3,125 | 75,359,484 | 38,598,000 | 2,031,000 | 2,330,624 | ||
| 4 | Dec 30, 2025 04:38 | 3,146 | 70,658,728 | 33,912,000 | 6,981,000 | 2,510,848 | ||
| 5 | Dec 30, 2025 04:45 | 3,230 | 69,594,549 | 38,996,000 | 2,999,000 | 2,506,752 | ||
| 6 | Dec 30, 2025 04:45 | 3,302 | 67,968,056 | 39,936,000 | 2,995,000 | 2,383,872 | ||
| 7 | Dec 30, 2025 04:38 | 3,306 | 74,661,049 | 40,981,000 | 1,999,000 | 2,330,624 | ||
| 8 | Dec 30, 2025 04:45 | 3,322 | 69,352,409 | 41,179,000 | 2,008,000 | 2,441,216 | ||
| 9 | Dec 30, 2025 04:45 | 3,349 | 71,785,742 | 42,525,000 | 1,012,000 | 2,445,312 | ||
| 10 | Dec 30, 2025 04:45 | 3,358 | 67,075,085 | 40,608,000 | 3,045,000 | 2,404,352 | ||
| 11 | Dec 30, 2025 04:45 | 3,383 | 70,019,632 | 40,975,000 | 2,998,000 | 2,416,640 | ||
| 12 | Dec 30, 2025 04:48 | 3,452 | 74,816,393 | 42,881,000 | 1,994,000 | 2,269,184 | ||
| 13 | Dec 30, 2025 04:44 | 3,456 | 72,897,815 | 41,928,000 | 2,994,000 | 2,457,600 | ||
| 14 | Dec 30, 2025 04:38 | 3,459 | 76,232,802 | 42,963,000 | 1,998,000 | 2,367,488 | ||
| 15 | Dec 30, 2025 04:38 | 3,469 | 72,724,849 | 43,090,000 | 2,004,000 | 2,424,832 | ||
| 16 | Dec 30, 2025 04:44 | 3,528 | 74,173,003 | 42,879,000 | 2,991,000 | 2,428,928 | ||
| 17 | Dec 30, 2025 04:44 | 3,528 | 81,736,030 | 42,875,000 | 2,991,000 | 2,293,760 | ||
| 18 | Dec 30, 2025 04:38 | 3,542 | 72,058,240 | 42,047,000 | 4,004,000 | 2,478,080 | ||
| 19 | Dec 30, 2025 04:48 | 3,568 | 69,553,250 | 41,337,000 | 5,041,000 | 2,445,312 | ||
| 20 | Dec 30, 2025 04:38 | 3,571 | 71,252,970 | 42,381,000 | 4,036,000 | 2,326,528 | ||
| 21 | Dec 30, 2025 04:48 | 3,606 | 76,823,307 | 45,875,000 | 997,000 | 2,379,776 | ||
| 22 | Dec 30, 2025 04:45 | 3,622 | 77,952,778 | 43,080,000 | 4,007,000 | 2,375,680 | ||
| 23 | Dec 30, 2025 04:45 | 3,682 | 73,559,676 | 45,878,000 | 1,994,000 | 2,297,856 | ||
| 24 | Dec 30, 2025 04:38 | 3,682 | 75,966,528 | 42,882,000 | 4,986,000 | 2,424,832 | ||
| 25 | Dec 30, 2025 04:45 | 3,717 | 72,093,511 | 43,288,000 | 5,033,000 | 2,367,488 | ||
| 26 | Dec 30, 2025 04:45 | 3,747 | 74,087,111 | 42,626,000 | 6,089,000 | 2,285,568 | ||
| 27 | Dec 30, 2025 04:45 | 3,843 | 73,336,242 | 45,958,000 | 3,996,000 | 2,342,912 |