Cross-Margin Liquidation Engine yenw0d

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 of trade_size * price_at_trade_time across all trades
  • Margin rule: liquidate if equity < total_notional / 100 after any price update

Input

One command per line on STDIN:

  • a <balance> – create account with USD balance (IDs start at 0, incrementing)
  • p <instrument_idx> <price> – set/update instrument price (0-indexed)
  • t <account_idx> <instrument_idx> <size> – trade size (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
Back to listMar 20, 2026 04:59Yuriy LyfenkoYuriy LyfenkoScore: 3,510Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 20, 2026 04:473,18466,190,55337,358,0004,038,0002,445,312
2Mar 20, 2026 04:593,19872,349,35836,502,0005,069,0002,400,256
3Mar 20, 2026 04:473,21669,277,74936,826,0004,976,0002,428,928
4Mar 20, 2026 04:593,27169,803,24638,476,0004,050,0002,400,256
5Mar 20, 2026 04:473,31767,723,94337,099,0006,016,0002,310,144
6Mar 20, 2026 04:473,34169,882,03238,379,0005,049,0002,359,296
7Mar 20, 2026 04:593,36167,285,31739,631,0004,064,0002,523,136
8Mar 20, 2026 04:593,37668,370,15440,895,0002,992,0002,375,680
9Mar 20, 2026 04:473,38372,918,27441,052,0002,932,0002,506,752
10Mar 20, 2026 04:473,39074,758,14238,065,0006,010,0002,510,848
11Mar 20, 2026 04:473,46675,642,31441,057,0004,005,0002,404,352
12Mar 20, 2026 04:473,46972,821,55439,084,0006,012,0002,494,464
13Mar 20, 2026 04:473,48873,103,49338,289,0007,053,0002,375,680
14Mar 20, 2026 04:473,48967,280,28641,330,0004,032,0002,359,296
15Mar 20, 2026 04:473,50473,955,74440,493,0005,061,0002,453,504
16Mar 20, 2026 04:473,51069,350,42943,601,0002,027,0002,392,064
17Mar 20, 2026 04:593,54971,735,64842,128,0004,012,0002,469,888
18Mar 20, 2026 04:593,55471,269,65642,187,0004,017,0002,416,640
19Mar 20, 2026 04:593,56974,060,17240,345,0006,051,0002,351,104
20Mar 20, 2026 04:473,58875,394,80143,606,0003,042,0002,367,488
21Mar 20, 2026 04:593,60872,003,98243,914,0002,994,0002,400,256
22Mar 20, 2026 04:593,61172,814,68843,942,0002,996,0002,457,600
23Mar 20, 2026 04:473,61471,576,40542,987,0003,998,0002,510,848
24Mar 20, 2026 04:473,61772,035,73643,015,0004,001,0002,400,256
25Mar 20, 2026 04:473,64668,315,16743,369,0004,034,0002,412,544
26Mar 20, 2026 04:593,67070,906,64542,640,0005,076,0002,408,448
27Mar 20, 2026 04:593,68771,773,07143,938,0003,994,0002,392,064
28Mar 20, 2026 04:473,74680,237,41144,641,0004,058,0002,359,296
29Mar 20, 2026 04:593,89581,556,87548,615,0002,025,0002,383,872
30Mar 20, 2026 04:473,94677,287,21044,252,0007,040,0002,334,720