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 04:28Josu San MartinJosu San MartinScore: 17,198Success
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 04:2815,363225,911,061191,732,0007,988,0002,408,448
2Dec 24, 2025 04:0415,810232,161,422201,518,0004,010,0002,445,312
3Dec 24, 2025 04:2816,373238,574,260202,856,0009,992,0002,469,888
4Dec 24, 2025 04:0416,428242,409,365209,558,0004,010,0002,535,424
5Dec 24, 2025 04:0416,492239,996,945207,387,0007,013,0002,330,624
6Dec 24, 2025 04:0417,015250,554,540216,192,0005,004,0002,387,968
7Dec 24, 2025 04:2817,058247,867,405213,758,0007,990,0002,408,448
8Dec 24, 2025 04:2817,151249,797,724217,961,0004,999,0002,461,696
9Dec 24, 2025 04:0417,160250,160,425219,073,0004,001,0002,433,024
10Dec 24, 2025 04:0417,198251,195,908218,567,0005,013,0002,486,272
11Dec 24, 2025 04:0417,301256,392,342215,916,0008,996,0002,482,176
12Dec 24, 2025 04:2817,359254,900,902218,680,0006,989,0002,400,256
13Dec 24, 2025 04:2817,598255,928,791215,785,00012,987,0002,490,368
14Dec 24, 2025 04:0417,645256,857,968218,363,00011,018,0002,359,296
15Dec 24, 2025 04:0417,708254,907,219221,199,0009,008,0002,416,640
16Dec 24, 2025 04:2817,792256,350,004226,287,0005,006,0002,387,968
17Dec 24, 2025 04:2818,560264,755,829232,268,0009,010,0002,420,736
18Dec 24, 2025 04:2818,768273,762,309233,982,0009,999,0002,527,232