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 25, 2025 05:13Josu San MartinJosu San MartinScore: 13,714Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 04:0211,407179,388,677145,286,0003,005,0002,408,448
2Dec 25, 2025 05:1611,435180,046,252145,666,0002,993,0002,465,792
3Dec 25, 2025 04:0211,643177,490,862145,341,0006,014,0002,449,408
4Dec 25, 2025 04:0211,709178,794,128147,210,0005,007,0002,375,680
5Dec 25, 2025 04:0211,803183,812,542146,423,0007,020,0002,428,928
6Dec 25, 2025 04:0211,907185,112,542150,799,0003,994,0002,310,144
7Dec 25, 2025 04:0212,025181,280,243153,313,0003,006,0002,490,368
8Dec 25, 2025 05:1712,073184,502,444152,950,0003,998,0002,330,624
9Dec 25, 2025 05:1612,093184,526,910156,209,0001,001,0002,408,448
10Dec 25, 2025 04:0212,299185,392,939157,894,0001,998,0002,293,760
11Dec 25, 2025 05:1612,326184,692,086157,238,0003,004,0002,465,792
12Dec 25, 2025 04:0212,364192,362,808158,740,0001,996,0002,473,984
13Dec 25, 2025 04:0212,599198,964,362161,787,0001,997,0002,478,080
14Dec 25, 2025 04:0212,609188,937,549159,915,0003,997,0002,428,928
15Dec 25, 2025 05:1712,818196,970,324162,620,0004,015,0002,482,176
16Dec 25, 2025 05:1613,077200,335,965167,999,0001,999,0002,486,272
17Dec 25, 2025 05:1713,084200,114,804169,094,0001,000,0002,306,048
18Dec 25, 2025 05:1713,143196,692,015164,859,0005,994,0002,424,832
19Dec 25, 2025 04:0213,187203,175,706169,432,0002,005,0002,351,104
20Dec 25, 2025 04:0213,231198,700,608169,002,0003,000,0002,293,760
21Dec 25, 2025 04:0213,258198,140,468168,350,0004,008,0002,289,664
22Dec 25, 2025 04:0213,262194,343,505167,399,0005,011,0002,498,560
23Dec 25, 2025 04:0213,280205,291,483167,655,0004,989,0002,400,256
24Dec 25, 2025 04:0213,325200,413,442172,223,0001,001,0002,494,464
25Dec 25, 2025 05:1613,368205,735,757171,789,0001,997,0002,371,584
26Dec 25, 2025 04:0213,399197,669,479170,181,0004,004,0002,277,376
27Dec 25, 2025 05:1513,444205,012,297171,776,0002,996,0002,281,472
28Dec 25, 2025 05:1613,521204,197,096171,784,0003,994,0002,490,368
29Dec 25, 2025 05:1613,537199,917,928170,981,0004,999,0002,355,200
30Dec 25, 2025 05:1513,548200,104,553173,127,0003,002,0002,514,944
31Dec 25, 2025 04:0213,582203,629,528172,558,0004,012,0002,510,848
32Dec 25, 2025 04:0213,620204,955,612173,055,0004,001,0002,371,584
33Dec 25, 2025 04:0213,676207,830,489174,786,0002,996,0002,277,376
34Dec 25, 2025 04:0113,692212,046,325172,000,0006,000,0002,510,848
35Dec 25, 2025 05:1613,709206,602,764176,220,0002,002,0002,510,848
36Dec 25, 2025 04:0213,712200,419,734174,246,0004,005,0002,396,160
37Dec 25, 2025 05:1713,714204,030,750175,275,0003,004,0002,355,200
38Dec 25, 2025 04:0213,867203,578,495178,267,0002,003,0002,326,528
39Dec 25, 2025 05:1714,034207,830,990178,432,0004,009,0002,514,944
40Dec 25, 2025 04:0214,072207,969,125178,933,0003,998,0002,420,736
41Dec 25, 2025 04:0214,072211,061,434179,932,0002,998,0002,478,080
42Dec 25, 2025 05:1714,136214,393,371176,783,0006,991,0002,293,760
43Dec 25, 2025 04:0214,169211,849,558180,196,0004,004,0002,441,216
44Dec 25, 2025 04:0214,185210,115,327180,392,0004,008,0002,301,952
45Dec 25, 2025 05:1714,228209,752,851180,970,0003,999,0002,408,448
46Dec 25, 2025 05:1514,234216,380,463180,044,0005,001,0002,306,048
47Dec 25, 2025 04:0214,260209,479,447184,383,0001,002,0002,433,024
48Dec 25, 2025 05:1614,435224,286,622182,664,0004,990,0002,289,664
49Dec 25, 2025 04:0214,562224,137,156186,303,0003,004,0002,449,408
50Dec 25, 2025 05:1714,624215,654,831186,104,0004,002,0002,269,184
51Dec 25, 2025 04:0114,660219,362,438189,574,0001,003,0002,269,184
52Dec 25, 2025 05:1714,672215,853,163185,738,0004,992,0002,330,624
53Dec 25, 2025 04:0214,739226,682,063185,591,0006,019,0002,527,232
54Dec 25, 2025 05:1314,811218,545,284187,533,0005,014,0002,420,736
55Dec 25, 2025 05:1714,836219,971,707189,866,0002,997,0002,310,144
56Dec 25, 2025 05:1614,889222,168,341191,550,0002,005,0002,523,136
57Dec 25, 2025 04:0214,903218,275,310191,740,0001,997,0002,293,760
58Dec 25, 2025 05:1714,934220,824,169191,144,0003,002,0002,412,544
59Dec 25, 2025 04:0214,954224,367,326193,398,0001,002,0002,379,776
60Dec 25, 2025 05:1515,026225,575,856193,329,0002,003,0002,494,464
61Dec 25, 2025 04:0215,211229,364,168193,743,0003,994,0002,482,176
62Dec 25, 2025 04:0115,246221,505,785195,198,0003,003,0002,347,008
63Dec 25, 2025 04:0215,291227,547,176191,791,0006,992,0002,371,584
64Dec 25, 2025 05:1315,301220,860,611193,917,0004,997,0002,404,352
65Dec 25, 2025 05:1515,570228,515,068199,399,0003,006,0002,342,912
66Dec 25, 2025 04:0215,598234,207,200199,776,0002,996,0002,273,280
67Dec 25, 2025 04:0215,807231,328,456199,474,0006,014,0002,314,240
68Dec 25, 2025 05:1516,301239,586,153201,912,0009,995,0002,359,296
69Dec 25, 2025 05:1616,407240,267,598209,290,0004,005,0002,510,848
70Dec 25, 2025 05:1316,468239,094,176210,083,0004,001,0002,428,928
71Dec 25, 2025 04:0216,695244,599,921215,035,0002,000,0002,301,952
72Dec 25, 2025 05:1617,943260,141,871232,259,0001,001,0002,527,232