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:31Josu San MartinJosu San MartinScore: 3,792Success
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:323,27772,439,57639,559,0003,043,0002,457,600
2Dec 29, 2025 14:443,30568,851,31539,970,0002,997,0002,347,008
3Dec 29, 2025 14:373,32370,104,79437,172,0006,027,0002,379,776
4Dec 29, 2025 14:323,32477,775,47140,202,0003,015,0002,433,024
5Dec 29, 2025 14:383,40370,722,22840,212,0004,021,0002,293,760
6Dec 29, 2025 14:373,40669,979,02840,257,0004,025,0002,469,888
7Dec 29, 2025 14:333,40677,292,59239,242,0005,031,0002,396,160
8Dec 29, 2025 14:323,41266,470,24442,341,0002,016,0002,338,816
9Dec 29, 2025 14:323,43170,445,90641,561,0003,041,0002,457,600
10Dec 29, 2025 14:313,43771,690,87843,664,0001,015,0002,375,680
11Dec 29, 2025 14:303,45369,254,79942,891,0001,994,0002,453,504
12Dec 29, 2025 14:323,48175,611,69740,226,0005,028,0002,387,968
13Dec 29, 2025 14:403,51376,016,91141,605,0004,059,0002,342,912
14Dec 29, 2025 14:323,53673,575,20143,974,0001,998,0002,404,352
15Dec 29, 2025 14:413,55269,381,96042,160,0004,015,0002,457,600
16Dec 29, 2025 14:353,55673,071,17040,195,0006,029,0002,330,624
17Dec 29, 2025 14:383,56467,552,60940,288,0006,043,0002,469,888
18Dec 29, 2025 14:463,60572,936,88943,876,0002,991,0002,273,280
19Dec 29, 2025 14:333,62484,061,85643,100,0004,009,0002,375,680
20Dec 29, 2025 14:413,62578,692,89443,118,0004,010,0002,367,488
21Dec 29, 2025 14:323,62973,061,13242,164,0005,019,0002,375,680
22Dec 29, 2025 14:353,68277,850,45345,871,0001,994,0002,334,720
23Dec 29, 2025 14:413,70673,337,40343,161,0005,018,0002,387,968
24Dec 29, 2025 14:333,74374,674,42045,624,0003,041,0002,396,160
25Dec 29, 2025 14:433,78084,151,27347,139,0002,005,0002,486,272
26Dec 29, 2025 14:323,79271,604,03444,270,0005,030,0002,334,720
27Dec 29, 2025 14:433,80474,497,21045,416,0004,037,0002,351,104
28Dec 29, 2025 14:323,83679,925,35947,877,0001,994,0002,490,368
29Dec 29, 2025 14:313,83879,491,83745,899,0003,991,0002,437,120
30Dec 29, 2025 14:463,85076,722,70348,050,0002,002,0002,510,848
31Dec 29, 2025 14:383,86683,295,09646,240,0004,020,0002,408,448
32Dec 29, 2025 14:463,87274,596,64846,313,0004,027,0002,375,680
33Dec 29, 2025 14:443,90180,848,08046,661,0004,057,0002,342,912
34Dec 29, 2025 14:313,90475,789,23448,722,0002,030,0002,375,680
35Dec 29, 2025 14:433,93278,745,11248,110,0003,006,0002,379,776
36Dec 29, 2025 14:333,94075,942,60749,207,0002,008,0002,289,664
37Dec 29, 2025 14:303,94579,298,73549,275,0002,011,0002,310,144
38Dec 29, 2025 14:353,96479,601,49047,495,0004,042,0002,371,584
39Dec 29, 2025 14:323,97877,395,14849,720,0001,988,0002,273,280
40Dec 29, 2025 14:313,99576,747,65247,864,0004,073,0002,375,680
41Dec 29, 2025 14:334,00978,200,04949,116,0003,007,0002,330,624
42Dec 29, 2025 14:304,02377,013,53649,278,0003,017,0002,392,064
43Dec 29, 2025 14:324,03975,804,47450,488,0002,019,0002,285,568
44Dec 29, 2025 14:334,06683,048,00948,868,0003,989,0002,293,760
45Dec 29, 2025 14:324,07377,524,00147,960,0004,995,0002,519,040
46Dec 29, 2025 14:444,09578,683,45551,225,0002,008,0002,433,024
47Dec 29, 2025 14:374,10880,910,39450,381,0003,022,0002,330,624
48Dec 29, 2025 14:404,18279,137,21949,335,0005,034,0002,273,280
49Dec 29, 2025 14:314,20780,999,97852,672,0002,025,0002,392,064
50Dec 29, 2025 14:404,22785,935,52053,956,000999,0002,387,968
51Dec 29, 2025 14:314,38285,430,05153,967,0002,998,0002,334,720