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 listDec 29, 2025 06:08Josu San MartinJosu San MartinScore: 3,974Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 06:083,25666,752,62637,285,0005,038,0002,424,832
2Dec 29, 2025 06:063,33271,796,26940,296,0003,022,0002,379,776
3Dec 29, 2025 06:093,47970,626,91241,205,0004,020,0002,289,664
4Dec 29, 2025 06:073,50272,361,56743,498,0002,023,0002,420,736
5Dec 29, 2025 06:053,54773,767,72940,092,0006,013,0002,506,752
6Dec 29, 2025 06:093,56366,361,29343,297,0003,020,0002,269,184
7Dec 29, 2025 06:073,62081,582,16443,052,0004,004,0002,428,928
8Dec 29, 2025 06:053,62776,127,30244,142,0003,009,0002,293,760
9Dec 29, 2025 06:063,63675,649,85644,249,0003,017,0002,383,872
10Dec 29, 2025 06:093,65670,012,21545,503,0002,022,0002,424,832
11Dec 29, 2025 06:093,70279,879,40747,126,0001,002,0002,363,392
12Dec 29, 2025 06:093,78976,916,67045,240,0004,021,0002,265,088
13Dec 29, 2025 06:083,79574,805,97945,303,0004,027,0002,347,008
14Dec 29, 2025 05:483,85179,663,29646,056,0004,004,0002,375,680
15Dec 29, 2025 06:053,87078,894,11249,302,0001,006,0002,412,544
16Dec 29, 2025 06:063,89381,055,33347,570,0003,036,0002,473,984
17Dec 29, 2025 05:483,89876,864,89846,624,0004,054,0002,330,624
18Dec 29, 2025 05:483,97373,475,57951,654,00002,306,048
19Dec 29, 2025 05:483,97477,371,61748,629,0003,039,0002,523,136
20Dec 29, 2025 06:063,99777,454,74546,967,0004,996,0002,289,664
21Dec 29, 2025 06:094,01377,042,26351,171,0001,003,0002,293,760
22Dec 29, 2025 06:054,01983,252,77250,237,0002,009,0002,461,696
23Dec 29, 2025 05:484,02379,416,40248,282,0004,023,0002,494,464
24Dec 29, 2025 06:054,05577,488,28850,690,0002,027,0002,265,088
25Dec 29, 2025 06:084,05786,547,20951,725,0001,014,0002,457,600
26Dec 29, 2025 06:074,08281,310,64651,065,0002,002,0002,408,448
27Dec 29, 2025 06:054,14487,258,66951,874,0001,995,0002,502,656
28Dec 29, 2025 06:064,18074,291,87950,311,0004,024,0002,457,600
29Dec 29, 2025 05:474,22886,053,90251,964,0002,997,0002,330,624
30Dec 29, 2025 06:064,22983,059,25450,983,0003,998,0002,404,352
31Dec 29, 2025 06:084,23981,263,61251,104,0004,008,0002,371,584
32Dec 29, 2025 05:474,25879,862,52351,333,0004,026,0002,326,528
33Dec 29, 2025 06:084,28780,120,63153,705,0002,026,0002,322,432
34Dec 29, 2025 05:474,37588,013,54753,886,0002,993,0002,498,560
35Dec 29, 2025 05:484,41778,859,76455,411,0002,014,0002,318,336
36Dec 29, 2025 06:084,50783,944,68755,555,0003,030,0002,502,656