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 26, 2025 23:39Josu San MartinJosu San MartinScore: 5,297Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 23:394,08584,146,50350,100,0003,006,0002,351,104
2Dec 27, 2025 15:284,26683,743,44351,430,0004,033,0002,269,184
3Dec 27, 2025 15:524,49379,572,55955,390,0003,021,0002,277,376
4Dec 27, 2025 18:584,62588,465,74754,108,0006,012,0002,514,944
5Dec 26, 2025 23:384,64793,413,92357,390,0003,020,0002,383,872
6Dec 27, 2025 18:584,66392,519,47857,586,0003,030,0002,355,200
7Dec 27, 2025 15:284,73888,181,42860,581,0001,009,0002,400,256
8Dec 27, 2025 15:244,75789,917,73360,838,000997,0002,359,296
9Dec 26, 2025 23:394,75794,036,03858,850,0002,992,0002,289,664
10Dec 27, 2025 18:584,77993,518,92060,128,0002,004,0002,277,376
11Dec 27, 2025 15:554,80191,629,50159,396,0003,020,0002,424,832
12Dec 27, 2025 16:164,82493,874,48962,706,00002,510,848
13Dec 27, 2025 15:284,88593,114,97960,480,0003,024,0002,478,080
14Dec 26, 2025 23:384,90993,457,13461,824,0001,994,0002,326,528
15Dec 27, 2025 15:244,969103,393,17358,538,0006,055,0002,355,200
16Dec 26, 2025 23:385,04086,786,98761,493,0004,032,0002,330,624
17Dec 26, 2025 23:405,07996,302,52161,031,0005,002,0002,338,816
18Dec 26, 2025 23:395,08996,944,30461,146,0005,012,0002,404,352
19Dec 26, 2025 23:385,12890,323,07163,640,0003,030,0002,433,024
20Dec 27, 2025 15:285,14397,345,56862,865,0003,991,0002,383,872
21Dec 26, 2025 23:385,16493,010,07665,124,0002,003,0002,408,448
22Dec 26, 2025 23:385,16990,633,47565,187,0002,005,0002,416,640
23Dec 26, 2025 23:395,17991,993,74162,307,0005,024,0002,387,968
24Dec 26, 2025 23:385,19491,866,94366,514,0001,007,0002,514,944
25Dec 26, 2025 23:385,208103,001,91766,696,0001,010,0002,412,544
26Dec 27, 2025 15:245,22198,884,12864,884,0002,994,0002,355,200
27Dec 27, 2025 15:245,27398,366,70867,541,0001,008,0002,314,240
28Dec 27, 2025 16:165,286104,796,64064,677,0004,042,0002,371,584
29Dec 26, 2025 23:395,29797,345,27865,869,0002,994,0002,396,160
30Dec 27, 2025 15:295,29793,876,39564,869,0003,991,0002,420,736
31Dec 27, 2025 15:525,29799,362,91765,865,0002,993,0002,334,720
32Dec 27, 2025 15:245,29898,024,39462,881,0005,988,0002,437,120
33Dec 26, 2025 23:385,32692,114,27864,220,0005,017,0002,441,216
34Dec 26, 2025 23:395,33797,039,79963,343,0006,032,0002,502,656
35Dec 26, 2025 23:395,354100,423,56267,585,0002,017,0002,437,120
36Dec 27, 2025 15:245,41694,582,11168,397,0002,011,0002,326,528
37Dec 27, 2025 15:555,46993,601,22066,089,0005,006,0002,461,696
38Dec 26, 2025 23:385,499101,453,81667,466,0004,027,0002,306,048
39Dec 27, 2025 15:525,583101,845,82068,553,0004,032,0002,355,200
40Dec 26, 2025 23:385,59199,743,43667,641,0005,047,0002,445,312
41Dec 26, 2025 23:405,629104,070,70568,166,0005,012,0002,375,680
42Dec 27, 2025 15:285,635107,831,35269,239,0004,013,0002,342,912
43Dec 26, 2025 23:405,69698,508,10671,052,0003,002,0002,473,984
44Dec 26, 2025 23:385,728101,077,39969,427,0005,031,0002,510,848
45Dec 26, 2025 23:385,756105,286,22572,836,0001,995,0002,338,816
46Dec 27, 2025 15:285,773101,512,60972,044,0003,001,0002,301,952
47Dec 27, 2025 16:165,78698,568,60271,208,0004,011,0002,498,560
48Dec 26, 2025 23:385,80298,425,49372,412,0003,017,0002,514,944
49Dec 26, 2025 23:385,911105,208,01773,854,0002,994,0002,334,720
50Dec 27, 2025 15:555,989104,619,76074,863,0002,994,0002,478,080
51Dec 27, 2025 15:296,107109,029,35575,371,0004,019,0002,416,640
52Dec 26, 2025 23:396,125106,520,28176,606,0003,023,0002,330,624
53Dec 26, 2025 23:386,142112,057,96376,850,0002,994,0002,510,848
54Dec 26, 2025 23:386,329106,287,95577,261,0005,016,0002,514,944
55Dec 26, 2025 23:386,457109,190,93979,943,0003,997,0002,441,216
56Dec 26, 2025 23:396,532106,345,01080,923,0003,996,0002,514,944
57Dec 27, 2025 15:297,020113,867,53584,242,0007,020,0002,510,848