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 14:37Josu San MartinJosu San MartinScore: 3,716Success
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 06:282,94067,508,71536,204,0002,011,0002,375,680
2Dec 29, 2025 14:353,25571,008,93638,282,0004,029,0002,469,888
3Dec 29, 2025 06:273,33072,074,33641,282,0002,013,0002,453,504
4Dec 29, 2025 14:353,37569,339,39241,884,0001,994,0002,363,392
5Dec 29, 2025 14:353,41271,940,17239,318,0005,040,0002,437,120
6Dec 29, 2025 14:323,43473,848,52042,614,0002,029,0002,293,760
7Dec 29, 2025 06:243,46274,679,13241,009,0004,000,0002,363,392
8Dec 29, 2025 14:353,48369,198,72141,255,0004,024,0002,387,968
9Dec 29, 2025 06:233,48767,029,69140,292,0005,036,0002,347,008
10Dec 29, 2025 14:383,51774,246,11442,672,0003,048,0002,367,488
11Dec 29, 2025 14:433,53073,770,93642,901,0002,993,0002,490,368
12Dec 29, 2025 14:323,54379,849,56243,059,0003,004,0002,310,144
13Dec 29, 2025 06:213,54470,283,00042,061,0004,005,0002,408,448
14Dec 29, 2025 14:443,56076,379,33840,240,0006,036,0002,404,352
15Dec 29, 2025 14:403,56372,173,00644,310,0002,014,0002,297,856
16Dec 29, 2025 14:353,56568,515,81941,306,0005,037,0002,457,600
17Dec 29, 2025 14:353,57177,203,02042,389,0004,037,0002,363,392
18Dec 29, 2025 06:223,58268,316,99944,541,0002,024,0002,351,104
19Dec 29, 2025 06:283,58468,400,83743,549,0003,038,0002,379,776
20Dec 29, 2025 14:433,58569,374,45744,576,0002,026,0002,408,448
21Dec 29, 2025 14:353,59373,514,76044,673,0002,030,0002,326,528
22Dec 29, 2025 06:253,60574,753,00644,877,0001,994,0002,387,968
23Dec 29, 2025 14:433,60771,258,31943,900,0002,993,0002,424,832
24Dec 29, 2025 06:203,61073,547,09843,932,0002,995,0002,379,776
25Dec 29, 2025 14:443,61771,223,59245,016,0002,000,0002,306,048
26Dec 29, 2025 14:463,63577,069,20743,238,0004,022,0002,428,928
27Dec 29, 2025 06:283,66270,008,41943,551,0004,051,0002,453,504
28Dec 29, 2025 06:213,68779,016,70545,938,0001,997,0002,347,008
29Dec 29, 2025 14:333,68970,896,10244,963,0002,997,0002,289,664
30Dec 29, 2025 06:253,69075,193,06345,977,0001,999,0002,490,368
31Dec 29, 2025 06:243,69677,880,42546,041,0002,001,0002,387,968
32Dec 29, 2025 06:283,70172,479,94845,112,0003,007,0002,400,256
33Dec 29, 2025 14:443,70774,906,28046,188,0002,008,0002,404,352
34Dec 29, 2025 06:203,71379,034,98944,251,0004,022,0002,363,392
35Dec 29, 2025 06:263,71575,723,42646,286,0002,012,0002,273,280
36Dec 29, 2025 06:283,71577,442,11743,262,0005,030,0002,269,184
37Dec 29, 2025 06:263,71673,443,29443,274,0005,031,0002,408,448
38Dec 29, 2025 14:333,76079,961,39447,888,000997,0002,359,296
39Dec 29, 2025 14:343,76171,360,33746,893,0001,995,0002,310,144
40Dec 29, 2025 14:413,76274,615,96345,912,0002,994,0002,408,448
41Dec 29, 2025 06:283,77179,873,54447,023,0002,000,0002,306,048
42Dec 29, 2025 14:373,77575,158,85244,073,0005,008,0002,420,736
43Dec 29, 2025 06:203,77674,718,31848,092,0001,001,0002,506,752
44Dec 29, 2025 14:403,80673,998,48046,454,0003,029,0002,289,664
45Dec 29, 2025 14:463,80875,742,90547,479,0002,020,0002,387,968
46Dec 29, 2025 14:333,81480,475,13448,567,0001,011,0002,334,720
47Dec 29, 2025 06:223,83874,240,39647,893,0001,995,0002,449,408
48Dec 29, 2025 14:383,85075,932,99446,050,0004,004,0002,342,912
49Dec 29, 2025 14:413,85780,691,17947,139,0003,008,0002,400,256
50Dec 29, 2025 06:283,85980,300,98045,153,0005,017,0002,457,600
51Dec 29, 2025 14:413,88277,166,24548,447,0002,018,0002,408,448
52Dec 29, 2025 14:323,91375,822,13346,884,0003,990,0002,297,856
53Dec 29, 2025 14:343,91478,698,46547,891,0002,993,0002,383,872
54Dec 29, 2025 06:283,92079,531,42946,958,0003,996,0002,363,392
55Dec 29, 2025 06:273,94782,520,91846,286,0005,031,0002,367,488
56Dec 29, 2025 14:353,94881,339,12645,283,0006,037,0002,342,912
57Dec 29, 2025 06:273,98677,482,34345,843,0005,979,0002,355,200
58Dec 29, 2025 06:233,99078,246,61047,879,0003,989,0002,326,528
59Dec 29, 2025 06:284,00972,606,40749,109,0003,006,0002,322,432
60Dec 29, 2025 06:224,04177,753,77949,499,0003,030,0002,326,528
61Dec 29, 2025 14:404,11773,799,97650,497,0003,029,0002,396,160
62Dec 29, 2025 14:374,11876,140,94550,510,0003,030,0002,412,544
63Dec 29, 2025 06:244,15078,512,57649,954,0003,996,0002,306,048
64Dec 29, 2025 06:234,16785,189,75349,156,0005,015,0002,293,760
65Dec 29, 2025 14:344,18681,593,81350,384,0004,030,0002,363,392
66Dec 29, 2025 06:254,22183,892,06053,871,000997,0002,416,640
67Dec 29, 2025 06:214,24181,550,62352,122,0003,007,0002,322,432
68Dec 29, 2025 14:374,32182,167,39554,163,0002,006,0002,359,296
69Dec 29, 2025 14:464,33580,480,42653,332,0003,018,0002,494,464
70Dec 29, 2025 06:264,40885,255,93053,286,0004,021,0002,506,752
71Dec 29, 2025 14:384,52986,703,20254,883,0003,991,0002,494,464
72Dec 29, 2025 14:354,66987,245,66057,659,0003,034,0002,433,024