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 01:18Josu San MartinJosu San MartinScore: 4,174Success
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 01:063,61275,597,33943,965,0002,997,0002,510,848
2Dec 29, 2025 01:083,70973,987,17442,185,0006,026,0002,412,544
3Dec 29, 2025 01:073,76178,498,56345,904,0002,993,0002,473,984
4Dec 29, 2025 01:083,77675,049,58946,084,0003,005,0002,465,792
5Dec 29, 2025 01:153,79079,235,84345,249,0004,022,0002,469,888
6Dec 29, 2025 01:083,79880,410,61249,372,00002,404,352
7Dec 29, 2025 01:133,81781,728,05548,611,0001,012,0002,408,448
8Dec 29, 2025 01:153,85479,449,59347,091,0003,005,0002,387,968
9Dec 29, 2025 01:093,85677,382,45645,110,0005,012,0002,416,640
10Dec 29, 2025 01:173,90078,726,72148,667,0002,027,0002,461,696
11Dec 29, 2025 01:143,93082,382,35447,085,0004,007,0002,383,872
12Dec 29, 2025 01:173,94879,150,54850,316,0001,006,0002,457,600
13Dec 29, 2025 01:113,94878,218,88050,312,0001,006,0002,482,176
14Dec 29, 2025 01:143,96681,090,75246,499,0005,054,0002,412,544
15Dec 29, 2025 01:093,96982,485,61349,570,0002,023,0002,490,368
16Dec 29, 2025 01:163,99177,870,85047,888,0003,990,0002,457,600
17Dec 29, 2025 01:083,99976,579,76743,986,0007,997,0002,269,184
18Dec 29, 2025 01:104,01475,532,47448,164,0004,013,0002,347,008
19Dec 29, 2025 01:114,11385,925,74352,461,0001,008,0002,478,080
20Dec 29, 2025 01:164,12379,351,36950,570,0003,034,0002,363,392
21Dec 29, 2025 01:084,12483,075,61053,607,00002,301,952
22Dec 29, 2025 01:074,14483,764,85549,886,0003,990,0002,412,544
23Dec 29, 2025 01:194,17478,041,92654,267,00002,347,008
24Dec 29, 2025 01:124,18386,662,10952,363,0002,013,0002,469,888
25Dec 29, 2025 01:154,19684,339,43251,514,0003,030,0002,408,448
26Dec 29, 2025 01:134,21181,521,26251,757,0002,986,0002,387,968
27Dec 29, 2025 01:084,24883,791,34154,218,0001,004,0002,359,296
28Dec 29, 2025 01:164,26777,264,81254,462,0001,008,0002,494,464
29Dec 29, 2025 01:064,29484,991,20752,835,0002,990,0002,338,816
30Dec 29, 2025 01:184,31187,393,77254,037,0002,001,0002,330,624
31Dec 29, 2025 01:124,37486,976,74954,863,0001,995,0002,457,600
32Dec 29, 2025 01:194,40581,544,84551,239,0006,028,0002,379,776
33Dec 29, 2025 01:074,40882,418,06253,284,0004,021,0002,465,792
34Dec 29, 2025 01:104,42579,109,87257,527,00002,527,232
35Dec 29, 2025 01:114,48686,584,48254,297,0004,022,0002,424,832
36Dec 29, 2025 01:104,48881,615,78155,321,0003,017,0002,502,656
37Dec 29, 2025 01:124,49883,598,40055,453,0003,024,0002,428,928
38Dec 29, 2025 01:134,61686,303,78859,006,0001,000,0002,314,240
39Dec 29, 2025 01:144,63586,325,08955,234,0005,021,0002,514,944
40Dec 29, 2025 01:064,63689,841,11757,259,0003,013,0002,437,120
41Dec 29, 2025 01:174,64483,456,14560,374,00002,273,280
42Dec 29, 2025 01:194,65084,875,00657,424,0003,022,0002,441,216
43Dec 29, 2025 01:184,81891,673,97460,615,0002,020,0002,351,104
44Dec 29, 2025 01:094,83391,296,35458,843,0003,989,0002,506,752
45Dec 29, 2025 01:185,18194,953,82065,345,0002,010,0002,396,160