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 15:56Josu San MartinJosu San MartinScore: 3,763Success
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 15:453,12769,674,37636,582,0004,064,0002,404,352
2Dec 29, 2025 15:473,13367,946,16339,710,0001,018,0002,260,992
3Dec 29, 2025 15:443,22168,995,13836,886,0004,984,0002,342,912
4Dec 29, 2025 15:453,29874,147,07041,881,000997,0002,379,776
5Dec 29, 2025 16:103,34171,012,83941,419,0002,020,0002,404,352
6Dec 29, 2025 15:473,38776,260,79841,031,0003,002,0002,400,256
7Dec 29, 2025 15:563,40371,584,43739,209,0005,026,0002,465,792
8Dec 29, 2025 16:033,42166,919,75643,459,0001,010,0002,387,968
9Dec 29, 2025 15:593,45674,524,45739,934,0004,991,0002,269,184
10Dec 29, 2025 16:043,46570,801,28542,046,0003,003,0002,424,832
11Dec 29, 2025 16:093,48274,082,24642,251,0003,017,0002,338,816
12Dec 29, 2025 15:493,48770,214,15144,325,0001,007,0002,453,504
13Dec 29, 2025 16:103,49469,711,40943,406,0002,018,0002,383,872
14Dec 29, 2025 15:473,50673,068,14140,511,0005,063,0002,486,272
15Dec 29, 2025 15:503,52870,924,68242,879,0002,991,0002,404,352
16Dec 29, 2025 16:013,54273,336,90445,047,0001,001,0002,404,352
17Dec 29, 2025 15:473,55178,883,59343,149,0003,010,0002,428,928
18Dec 29, 2025 16:073,55676,231,99643,215,0003,015,0002,416,640
19Dec 29, 2025 15:473,60578,155,49446,868,00002,510,848
20Dec 29, 2025 15:443,60577,138,35844,875,0001,994,0002,301,952
21Dec 29, 2025 15:473,60671,636,97942,890,0003,989,0002,457,600
22Dec 29, 2025 16:033,60676,838,24444,878,0001,994,0002,310,144
23Dec 29, 2025 15:443,60978,284,01443,927,0002,995,0002,367,488
24Dec 29, 2025 16:063,61075,939,06842,942,0003,994,0002,318,336
25Dec 29, 2025 16:103,61175,656,63441,952,0004,994,0002,469,888
26Dec 29, 2025 16:013,62471,583,54945,110,0002,004,0002,437,120
27Dec 29, 2025 15:443,62778,618,09444,137,0003,009,0002,437,120
28Dec 29, 2025 15:503,68974,452,38740,964,0006,993,0002,441,216
29Dec 29, 2025 16:073,70683,564,51944,168,0004,015,0002,367,488
30Dec 29, 2025 15:443,71778,162,41247,310,0001,006,0002,359,296
31Dec 29, 2025 15:583,72376,313,46646,384,0002,016,0002,363,392
32Dec 29, 2025 15:443,73469,686,29145,506,0003,033,0002,342,912
33Dec 29, 2025 16:063,73769,639,17143,524,0005,061,0002,404,352
34Dec 29, 2025 15:593,76377,577,00446,886,0002,038,0002,322,432
35Dec 29, 2025 16:043,76882,309,32645,984,0002,998,0002,306,048
36Dec 29, 2025 15:453,77275,239,06446,039,0003,002,0002,424,832
37Dec 29, 2025 16:033,85380,485,67647,083,0003,005,0002,400,256
38Dec 29, 2025 16:073,86279,948,22546,195,0004,017,0002,408,448
39Dec 29, 2025 16:093,87177,100,89944,289,0006,039,0002,510,848
40Dec 29, 2025 15:583,87373,675,97547,332,0003,021,0002,441,216
41Dec 29, 2025 15:443,88874,930,48247,506,0003,032,0002,314,240
42Dec 29, 2025 16:063,91081,506,30547,845,0002,990,0002,322,432
43Dec 29, 2025 15:473,91477,858,99047,894,0002,993,0002,416,640
44Dec 29, 2025 15:493,92075,774,87144,966,0005,995,0002,322,432
45Dec 29, 2025 15:473,94279,236,81449,236,0002,009,0002,342,912
46Dec 29, 2025 15:523,95175,715,45448,340,0003,021,0002,424,832
47Dec 29, 2025 15:473,95477,545,45447,369,0004,031,0002,273,280
48Dec 29, 2025 16:013,99682,876,73547,958,0003,996,0002,293,760
49Dec 29, 2025 15:503,99685,093,36649,947,0001,997,0002,441,216
50Dec 29, 2025 15:444,02182,484,02148,255,0004,021,0002,502,656
51Dec 29, 2025 15:594,02977,976,04148,344,0004,028,0002,424,832
52Dec 29, 2025 16:044,04180,961,58349,497,0003,030,0002,396,160
53Dec 29, 2025 15:554,04373,632,93950,534,0002,021,0002,367,488
54Dec 29, 2025 15:554,17482,547,82050,247,0004,019,0002,367,488
55Dec 29, 2025 15:534,20181,973,84551,575,0003,033,0002,289,664
56Dec 29, 2025 15:494,22980,121,48850,976,0003,998,0002,490,368
57Dec 29, 2025 15:444,25180,014,11650,238,0005,023,0002,318,336
58Dec 29, 2025 15:554,32498,141,51751,193,0005,018,0002,379,776
59Dec 29, 2025 16:094,42980,186,42354,546,0003,030,0002,408,448
60Dec 29, 2025 15:584,55983,693,58356,252,0003,013,0002,342,912
61Dec 29, 2025 15:524,65282,809,79956,450,0004,032,0002,441,216
62Dec 29, 2025 15:534,68586,408,35560,911,00002,371,584
63Dec 29, 2025 15:524,77889,362,43356,103,0006,011,0002,351,104
64Dec 29, 2025 15:534,90889,460,62459,818,0003,987,0002,285,568
65Dec 29, 2025 15:564,91489,130,64760,883,0002,994,0002,342,912
66Dec 29, 2025 15:565,029105,328,67858,446,0006,934,0002,367,488