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 24, 2025 22:10Josu San MartinJosu San MartinScore: 17,112Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 24, 2025 22:1113,331201,257,759163,281,00010,017,0002,277,376
2Dec 24, 2025 22:0813,788208,226,863171,238,0008,011,0002,469,888
3Dec 24, 2025 22:0813,966208,968,867173,537,0008,024,0002,400,256
4Dec 24, 2025 22:0814,437219,760,661174,708,00012,978,0002,449,408
5Dec 24, 2025 22:0814,842226,041,743182,952,0009,997,0002,408,448
6Dec 24, 2025 04:3415,138222,269,600189,798,0006,992,0002,383,872
7Dec 24, 2025 22:0815,325225,049,497191,212,0008,008,0002,523,136
8Dec 24, 2025 04:3415,383223,416,585190,978,0008,998,0002,355,200
9Dec 24, 2025 21:5415,465228,598,638193,045,0008,001,0002,519,040
10Dec 24, 2025 21:5415,612230,372,062194,959,0007,998,0002,318,336
11Dec 24, 2025 22:1115,654233,640,143198,489,0005,012,0002,494,464
12Dec 24, 2025 22:1015,844236,824,390197,973,0007,998,0002,465,792
13Dec 24, 2025 22:0816,058236,403,324202,732,0006,021,0002,494,464
14Dec 24, 2025 04:3416,139247,154,170197,817,00011,988,0002,330,624
15Dec 24, 2025 22:0816,337237,670,927201,357,00011,019,0002,523,136
16Dec 24, 2025 22:1016,428240,637,542206,547,0007,018,0002,498,560
17Dec 24, 2025 04:3416,448244,610,582204,832,0008,992,0002,318,336
18Dec 24, 2025 22:1016,471243,692,366203,117,00011,006,0002,400,256
19Dec 24, 2025 21:5416,796244,843,402209,338,0009,014,0002,482,176
20Dec 24, 2025 21:5416,814249,109,051214,577,0004,010,0002,281,472
21Dec 24, 2025 21:5416,913247,471,931216,873,0002,998,0002,347,008
22Dec 24, 2025 22:1117,112249,612,827220,451,0002,004,0002,347,008
23Dec 24, 2025 04:3417,133249,652,670216,737,0005,992,0002,383,872
24Dec 24, 2025 04:3417,187249,648,939216,417,0007,013,0002,412,544
25Dec 24, 2025 04:3417,236249,357,140217,061,0007,001,0002,523,136
26Dec 24, 2025 22:1017,330247,474,419220,280,0005,006,0002,359,296
27Dec 24, 2025 22:1117,370253,669,970219,813,0005,994,0002,342,912
28Dec 24, 2025 21:5417,401252,894,454218,208,0008,007,0002,383,872
29Dec 24, 2025 04:3417,450253,561,154220,854,0005,996,0002,400,256
30Dec 24, 2025 22:0817,595259,141,432224,736,0003,995,0002,310,144
31Dec 24, 2025 21:5417,747253,565,339227,721,0002,996,0002,473,984
32Dec 24, 2025 22:1117,749259,018,865223,719,0007,022,0002,310,144
33Dec 24, 2025 22:1017,799259,259,273225,380,0006,010,0002,445,312
34Dec 24, 2025 22:1017,910267,955,548222,843,0009,993,0002,514,944
35Dec 24, 2025 22:1118,286269,776,937231,726,0005,992,0002,392,064
36Dec 24, 2025 21:5418,670274,202,398232,726,0009,988,0002,297,856
37Dec 24, 2025 22:1018,870281,789,209237,295,0008,009,0002,523,136
38Dec 24, 2025 22:0819,320278,656,368242,154,0009,005,0002,281,472
39Dec 24, 2025 22:1019,373279,899,870242,858,0008,994,0002,351,104
40Dec 24, 2025 04:3419,515285,089,130245,705,0007,990,0002,400,256
41Dec 24, 2025 22:1020,090293,202,186255,168,0006,003,0002,461,696
42Dec 24, 2025 21:5420,521296,270,908258,779,0007,993,0002,310,144