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 06:44Josu San MartinJosu San MartinScore: 3,779Success
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 14:313,18963,389,38135,390,0006,066,0002,478,080
2Dec 29, 2025 14:313,19265,268,99939,471,0002,024,0002,306,048
3Dec 29, 2025 06:453,29969,469,60741,884,000997,0002,355,200
4Dec 29, 2025 06:433,33273,242,16340,294,0003,022,0002,306,048
5Dec 29, 2025 14:303,43973,072,35541,664,0003,048,0002,408,448
6Dec 29, 2025 06:383,53273,649,39143,914,0001,996,0002,383,872
7Dec 29, 2025 14:313,54072,854,24141,017,0005,002,0002,351,104
8Dec 29, 2025 06:463,54875,827,38140,111,0006,016,0002,420,736
9Dec 29, 2025 14:263,55575,719,20743,198,0003,013,0002,465,792
10Dec 29, 2025 06:453,56268,414,49542,280,0004,026,0002,502,656
11Dec 29, 2025 06:413,56575,893,18543,323,0003,022,0002,498,560
12Dec 29, 2025 14:263,56972,644,43742,359,0004,034,0002,379,776
13Dec 29, 2025 14:293,58071,696,87342,498,0004,047,0002,478,080
14Dec 29, 2025 14:313,58471,136,16142,546,0004,052,0002,355,200
15Dec 29, 2025 06:413,59474,569,15944,693,0002,031,0002,408,448
16Dec 29, 2025 06:383,60672,745,18944,881,0001,994,0002,318,336
17Dec 29, 2025 14:313,61276,203,02543,961,0002,997,0002,506,752
18Dec 29, 2025 06:393,63575,039,50344,244,0003,016,0002,342,912
19Dec 29, 2025 14:283,64370,705,95541,309,0006,045,0002,494,464
20Dec 29, 2025 14:253,64772,780,22842,371,0005,044,0002,465,792
21Dec 29, 2025 14:303,65471,047,15943,455,0004,042,0002,441,216
22Dec 29, 2025 14:283,67275,205,10246,727,0001,015,0002,478,080
23Dec 29, 2025 06:423,68473,933,54745,903,0001,995,0002,482,176
24Dec 29, 2025 06:403,70175,665,17143,106,0005,012,0002,297,856
25Dec 29, 2025 14:293,71080,752,64045,217,0003,014,0002,387,968
26Dec 29, 2025 14:273,72775,238,65944,415,0004,037,0002,383,872
27Dec 29, 2025 06:463,76476,164,88343,935,0004,992,0002,506,752
28Dec 29, 2025 06:393,77974,801,75447,122,0002,005,0002,416,640
29Dec 29, 2025 06:403,78376,514,75646,170,0003,011,0002,363,392
30Dec 29, 2025 14:283,79476,158,25247,312,0002,013,0002,420,736
31Dec 29, 2025 06:443,81173,933,77044,493,0005,056,0002,482,176
32Dec 29, 2025 06:393,81975,600,31947,616,0002,026,0002,453,504
33Dec 29, 2025 06:383,83775,241,09046,894,0002,993,0002,351,104
34Dec 29, 2025 14:303,84477,623,59745,971,0003,997,0002,326,528
35Dec 29, 2025 14:253,85480,335,16549,104,0001,002,0002,396,160
36Dec 29, 2025 14:313,90382,671,07348,710,0002,029,0002,379,776
37Dec 29, 2025 14:273,92377,279,35548,999,0001,999,0002,506,752
38Dec 29, 2025 06:453,92977,591,49247,070,0004,005,0002,400,256
39Dec 29, 2025 06:433,95774,377,68147,411,0004,034,0002,342,912
40Dec 29, 2025 14:293,95875,393,72048,424,0003,026,0002,433,024
41Dec 29, 2025 14:273,96173,321,38449,478,0002,019,0002,338,816
42Dec 29, 2025 06:414,02180,935,99749,259,0003,015,0002,510,848
43Dec 29, 2025 06:424,04473,923,55450,551,0002,022,0002,494,464
44Dec 29, 2025 14:254,07080,982,07446,915,0005,989,0002,371,584
45Dec 29, 2025 14:314,07684,190,71947,991,0004,999,0002,519,040
46Dec 29, 2025 06:444,09581,224,46546,203,0007,030,0002,363,392
47Dec 29, 2025 06:404,14480,761,56850,875,0002,992,0002,510,848
48Dec 29, 2025 06:444,14479,853,75650,885,0002,993,0002,473,984
49Dec 29, 2025 06:424,20080,059,06751,570,0003,033,0002,457,600
50Dec 29, 2025 14:314,22687,119,81351,939,0002,996,0002,314,240
51Dec 29, 2025 14:314,23681,401,78249,056,0006,006,0002,408,448
52Dec 29, 2025 06:464,30081,674,12653,900,0001,996,0002,355,200
53Dec 29, 2025 06:434,38983,028,05451,049,0006,005,0002,314,240
54Dec 29, 2025 14:264,65484,036,42957,483,0003,025,0002,445,312