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 03:36Josu San MartinJosu San MartinScore: 4,311Success
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 03:453,63170,669,11642,186,0005,022,0002,269,184
2Dec 29, 2025 03:443,63575,843,60944,237,0003,016,0002,478,080
3Dec 29, 2025 03:203,72677,380,37347,425,0001,009,0002,457,600
4Dec 29, 2025 03:343,80878,909,93747,479,0002,020,0002,367,488
5Dec 29, 2025 03:273,81573,504,63148,589,0001,012,0002,363,392
6Dec 29, 2025 03:223,85378,218,35849,088,0001,001,0002,375,680
7Dec 29, 2025 03:283,86973,097,53949,295,0001,006,0002,457,600
8Dec 29, 2025 03:263,88578,105,40047,478,0003,030,0002,383,872
9Dec 29, 2025 03:323,89881,285,00545,611,0005,067,0002,498,560
10Dec 29, 2025 03:353,90079,417,62648,671,0002,027,0002,437,120
11Dec 29, 2025 03:243,90185,150,91049,695,0001,014,0002,396,160
12Dec 29, 2025 03:343,93174,534,26244,094,0007,015,0002,412,544
13Dec 29, 2025 03:403,97584,719,48850,659,0001,013,0002,375,680
14Dec 29, 2025 03:293,98973,340,36346,869,0004,986,0002,510,848
15Dec 29, 2025 03:273,99075,419,24347,882,0003,990,0002,367,488
16Dec 29, 2025 03:284,01778,904,24349,212,0003,013,0002,506,752
17Dec 29, 2025 03:334,02078,812,92149,244,0003,014,0002,342,912
18Dec 29, 2025 03:394,03478,844,79650,425,0002,017,0002,359,296
19Dec 29, 2025 03:304,03577,516,23947,409,0005,043,0002,437,120
20Dec 29, 2025 03:434,04679,080,49150,578,0002,023,0002,367,488
21Dec 29, 2025 03:424,06883,117,90051,892,000997,0002,379,776
22Dec 29, 2025 03:384,07282,117,56448,944,0003,995,0002,514,944
23Dec 29, 2025 03:304,07682,232,17349,991,0002,999,0002,498,560
24Dec 29, 2025 03:404,09576,195,98651,229,0002,009,0002,498,560
25Dec 29, 2025 03:444,10677,840,31051,359,0002,014,0002,383,872
26Dec 29, 2025 03:354,11177,058,66351,422,0002,016,0002,457,600
27Dec 29, 2025 03:404,12679,900,88449,594,0004,048,0002,473,984
28Dec 29, 2025 03:204,15582,481,23949,009,0005,001,0002,277,376
29Dec 29, 2025 03:214,16381,387,16250,116,0004,009,0002,347,008
30Dec 29, 2025 01:254,17178,819,34849,204,0005,020,0002,441,216
31Dec 29, 2025 03:214,18388,092,95549,346,0005,035,0002,392,064
32Dec 29, 2025 01:254,18482,464,74653,383,0001,007,0002,404,352
33Dec 29, 2025 01:254,22281,400,57053,892,000998,0002,392,064
34Dec 29, 2025 03:394,22877,826,53651,964,0002,997,0002,420,736
35Dec 29, 2025 03:314,23279,337,17549,009,0006,001,0002,400,256
36Dec 29, 2025 03:264,26584,794,83050,408,0005,040,0002,441,216
37Dec 29, 2025 03:294,27282,270,48153,511,0002,019,0002,330,624
38Dec 29, 2025 03:454,27281,309,14850,485,0005,048,0002,342,912
39Dec 29, 2025 03:224,27582,762,92752,541,0003,031,0002,269,184
40Dec 29, 2025 03:244,29882,370,27553,876,0001,995,0002,318,336
41Dec 29, 2025 03:284,31186,889,71853,040,0003,002,0002,445,312
42Dec 29, 2025 03:374,33380,401,28753,314,0003,017,0002,465,792
43Dec 29, 2025 03:424,33681,230,98852,338,0004,026,0002,375,680
44Dec 29, 2025 03:374,34281,670,55153,418,0003,023,0002,428,928
45Dec 29, 2025 03:224,34383,404,13251,421,0005,041,0002,293,760
46Dec 29, 2025 03:274,34582,775,96252,449,0004,034,0002,363,392
47Dec 29, 2025 03:254,37685,207,35950,905,0005,988,0002,498,560
48Dec 29, 2025 03:354,38477,154,37652,993,0003,999,0002,469,888
49Dec 29, 2025 03:384,39082,566,37852,064,0005,006,0002,314,240
50Dec 29, 2025 03:454,42583,863,40055,511,0002,018,0002,416,640
51Dec 29, 2025 03:384,46291,795,51953,007,0005,000,0002,404,352
52Dec 29, 2025 03:264,51585,294,69055,656,0003,035,0002,441,216
53Dec 29, 2025 03:234,55084,723,69455,142,0004,010,0002,367,488
54Dec 29, 2025 03:294,55883,503,17357,241,0002,008,0002,453,504
55Dec 29, 2025 03:344,56186,372,08757,279,0002,009,0002,478,080
56Dec 29, 2025 03:394,59286,300,32257,677,0002,023,0002,314,240
57Dec 29, 2025 03:444,59484,588,81356,684,0003,036,0002,486,272
58Dec 29, 2025 03:304,61186,753,89853,953,0005,994,0002,424,832
59Dec 29, 2025 03:324,62383,566,46254,087,0006,009,0002,445,312
60Dec 29, 2025 03:254,62987,856,36755,157,0005,014,0002,461,696
61Dec 29, 2025 03:374,63085,814,20454,174,0006,019,0002,498,560
62Dec 29, 2025 03:334,64287,841,79657,324,0003,017,0002,478,080
63Dec 29, 2025 03:214,64390,299,73059,354,0001,006,0002,363,392
64Dec 29, 2025 03:434,65484,819,08359,496,0001,008,0002,437,120
65Dec 29, 2025 03:304,65885,679,70556,516,0004,036,0002,412,544
66Dec 29, 2025 03:314,68287,437,24857,875,0002,993,0002,523,136
67Dec 29, 2025 03:324,69986,611,75759,082,0002,002,0002,330,624
68Dec 29, 2025 03:304,76391,901,86556,930,0004,993,0002,289,664
69Dec 29, 2025 03:434,77189,191,78860,022,0002,000,0002,490,368
70Dec 29, 2025 03:424,78785,099,79958,213,0004,014,0002,310,144
71Dec 29, 2025 03:204,81192,335,50660,525,0002,017,0002,494,464
72Dec 29, 2025 03:254,81691,244,18558,574,0004,039,0002,486,272
73Dec 29, 2025 03:364,82086,881,80356,591,0006,063,0002,433,024
74Dec 29, 2025 03:234,83789,949,78859,889,0002,994,0002,375,680
75Dec 29, 2025 03:334,86092,387,53761,176,0002,005,0002,396,160
76Dec 29, 2025 03:364,86384,489,28958,206,0005,017,0002,338,816
77Dec 29, 2025 03:234,91397,141,39662,869,000997,0002,281,472
78Dec 29, 2025 03:365,15295,159,06964,975,0001,999,0002,441,216
79Dec 29, 2025 03:315,23298,009,52365,017,0003,000,0002,416,640
80Dec 29, 2025 03:245,27294,512,71866,521,0002,015,0002,375,680
81Dec 29, 2025 03:305,28295,679,36566,652,0002,019,0002,375,680