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 25, 2025 05:13Josu San MartinJosu San MartinScore: 15,845Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 04:5413,119205,043,242164,522,0006,019,0002,375,680
2Dec 25, 2025 04:5414,135213,508,442180,765,0002,996,0002,412,544
3Dec 25, 2025 04:5614,371213,438,825183,823,0002,997,0002,490,368
4Dec 25, 2025 04:5514,662217,233,320184,615,0005,987,0002,289,664
5Dec 25, 2025 04:5714,751221,299,001189,772,0001,997,0002,347,008
6Dec 25, 2025 04:5714,972227,402,985191,646,0002,994,0002,490,368
7Dec 25, 2025 04:5315,063221,770,867194,826,000999,0002,473,984
8Dec 25, 2025 04:5615,666236,623,782198,648,0005,016,0002,523,136
9Dec 25, 2025 04:5515,712232,144,743198,253,0006,007,0002,514,944
10Dec 25, 2025 04:5415,783233,176,455200,181,0005,004,0002,420,736
11Dec 25, 2025 04:5515,845234,978,252196,992,0008,999,0002,363,392
12Dec 25, 2025 04:5315,984236,983,313204,799,0002,997,0002,375,680
13Dec 25, 2025 04:5716,211240,523,236206,749,0003,995,0002,523,136
14Dec 25, 2025 04:5616,575243,425,034215,472,00002,494,464
15Dec 25, 2025 04:5316,608243,649,663208,903,0006,996,0002,293,760
16Dec 25, 2025 05:1316,637244,428,100212,282,0004,005,0002,334,720
17Dec 25, 2025 04:5716,847244,485,640214,017,0005,000,0002,404,352
18Dec 25, 2025 05:1317,303248,081,261220,940,0003,998,0002,310,144
19Dec 25, 2025 04:5717,332253,416,437224,309,0001,001,0002,441,216
20Dec 25, 2025 04:5717,593259,058,639224,715,0003,994,0002,502,656
21Dec 25, 2025 05:1317,706257,240,040225,172,0005,003,0002,482,176