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 04:34Josu San MartinJosu San MartinScore: 4,452Success
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 02:023,34965,973,18341,513,0002,025,0002,490,368
2Dec 29, 2025 01:543,64272,889,83546,340,0001,007,0002,326,528
3Dec 29, 2025 01:503,68478,721,99946,899,000997,0002,461,696
4Dec 29, 2025 02:003,71878,331,14146,325,0002,014,0002,375,680
5Dec 29, 2025 04:353,77175,792,09646,017,0003,001,0002,506,752
6Dec 29, 2025 02:003,78580,296,14747,199,0002,008,0002,404,352
7Dec 29, 2025 01:573,80077,717,39746,374,0003,024,0002,322,432
8Dec 29, 2025 01:473,89880,828,57850,677,00002,519,040
9Dec 29, 2025 04:363,92577,864,22148,025,0003,001,0002,506,752
10Dec 29, 2025 01:483,95575,708,38748,394,0003,024,0002,314,240
11Dec 29, 2025 01:543,97882,667,95846,648,0005,070,0002,510,848
12Dec 29, 2025 01:443,97982,564,35149,700,0002,028,0002,445,312
13Dec 29, 2025 01:574,03873,890,06049,467,0003,028,0002,306,048
14Dec 29, 2025 02:024,07677,679,89148,988,0003,999,0002,347,008
15Dec 29, 2025 02:024,09777,546,30152,257,0001,004,0002,490,368
16Dec 29, 2025 02:014,09981,112,82051,274,0002,010,0002,486,272
17Dec 29, 2025 01:454,12376,221,57549,557,0004,045,0002,453,504
18Dec 29, 2025 01:564,15486,834,43552,001,0002,000,0002,265,088
19Dec 29, 2025 01:474,18482,245,97051,369,0003,021,0002,355,200
20Dec 29, 2025 01:564,20378,563,17252,615,0002,023,0002,506,752
21Dec 29, 2025 01:594,20479,232,84852,668,0001,987,0002,412,544
22Dec 29, 2025 01:574,23084,055,80851,989,0002,999,0002,400,256
23Dec 29, 2025 01:484,23884,882,59253,094,0002,003,0002,322,432
24Dec 29, 2025 01:554,24581,204,79354,177,0001,003,0002,490,368
25Dec 29, 2025 01:584,26378,409,34651,388,0004,030,0002,486,272
26Dec 29, 2025 04:374,26980,179,24554,489,0001,009,0002,457,600
27Dec 29, 2025 01:594,27885,194,58354,601,0001,011,0002,428,928
28Dec 29, 2025 02:014,28182,109,26854,644,0001,011,0002,445,312
29Dec 29, 2025 01:514,30581,668,67151,969,0003,997,0002,408,448
30Dec 29, 2025 04:344,33188,531,40053,289,0003,016,0002,420,736
31Dec 29, 2025 01:484,33577,510,40752,336,0004,025,0002,445,312
32Dec 29, 2025 02:004,36181,960,93252,638,0004,049,0002,506,752
33Dec 29, 2025 01:514,37782,696,64952,904,0003,992,0002,404,352
34Dec 29, 2025 01:584,37886,550,19854,922,0001,997,0002,392,064
35Dec 29, 2025 01:524,39893,589,81752,155,0005,014,0002,461,696
36Dec 29, 2025 01:474,40582,433,93155,261,0002,009,0002,330,624
37Dec 29, 2025 01:524,43989,148,60556,699,0001,012,0002,441,216
38Dec 29, 2025 01:504,45287,541,78855,883,0001,995,0002,310,144
39Dec 29, 2025 01:434,45384,628,32355,895,0001,996,0002,383,872
40Dec 29, 2025 04:374,46389,400,15453,021,0005,002,0002,519,040
41Dec 29, 2025 01:454,47683,168,84554,177,0004,013,0002,383,872
42Dec 29, 2025 01:554,49485,867,50356,404,0002,014,0002,396,160
43Dec 29, 2025 01:434,53184,686,08554,914,0003,993,0002,363,392
44Dec 29, 2025 01:514,53787,545,79656,976,0001,999,0002,367,488
45Dec 29, 2025 01:534,55888,686,18057,252,0002,008,0002,506,752
46Dec 29, 2025 01:544,56683,653,15357,341,0002,011,0002,383,872
47Dec 29, 2025 01:434,57683,438,60356,465,0003,024,0002,465,792
48Dec 29, 2025 02:014,60489,571,74859,852,00002,314,240
49Dec 29, 2025 01:444,60892,135,95456,905,0002,995,0002,293,760
50Dec 29, 2025 01:534,63384,416,02857,222,0003,011,0002,363,392
51Dec 29, 2025 01:464,63787,728,45155,260,0005,023,0002,383,872
52Dec 29, 2025 01:554,63884,845,00354,266,0006,029,0002,285,568
53Dec 29, 2025 01:464,67584,651,74058,781,0001,992,0002,441,216
54Dec 29, 2025 01:534,70791,690,28457,179,0004,012,0002,469,888
55Dec 29, 2025 02:024,70989,707,29555,199,0006,021,0002,502,656
56Dec 29, 2025 04:364,71684,164,91457,285,0004,020,0002,265,088
57Dec 29, 2025 01:464,72383,443,99360,390,0001,006,0002,478,080
58Dec 29, 2025 01:594,79185,171,79360,279,0002,009,0002,494,464
59Dec 29, 2025 04:364,79985,100,98957,354,0005,031,0002,392,064
60Dec 29, 2025 04:354,80781,754,06758,465,0004,032,0002,453,504
61Dec 29, 2025 02:024,81891,391,47659,609,0003,030,0002,457,600
62Dec 29, 2025 01:524,85589,051,88961,108,0002,003,0002,359,296
63Dec 29, 2025 01:584,85797,685,68860,129,0003,006,0002,486,272
64Dec 29, 2025 04:344,86092,025,23160,172,0003,008,0002,494,464
65Dec 29, 2025 04:374,86490,176,40861,231,0002,007,0002,523,136
66Dec 29, 2025 02:024,86589,542,52361,233,0002,007,0002,449,408
67Dec 29, 2025 01:444,87088,207,87061,297,0002,009,0002,285,568
68Dec 29, 2025 01:454,91490,149,66761,891,0001,996,0002,453,504
69Dec 29, 2025 04:354,93689,712,72060,164,0004,010,0002,416,640
70Dec 29, 2025 04:344,95790,799,51360,416,0004,027,0002,424,832
71Dec 29, 2025 02:025,02591,090,14462,306,0003,014,0002,355,200
72Dec 29, 2025 02:025,04392,592,08860,516,0005,043,0002,367,488
73Dec 29, 2025 01:505,04686,136,96463,576,0002,018,0002,433,024
74Dec 29, 2025 01:565,25195,368,29265,256,0003,011,0002,383,872
75Dec 29, 2025 02:025,577100,764,63767,464,0005,034,0002,387,968