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 01:27Josu San MartinJosu San MartinScore: 16,289Success
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 01:2613,492198,512,630171,391,0004,009,0002,412,544
2Dec 25, 2025 01:2314,401213,387,081181,209,0006,006,0002,383,872
3Dec 25, 2025 01:2614,674222,946,598183,773,0006,991,0002,502,656
4Dec 25, 2025 01:2714,960224,441,660190,469,0004,009,0002,367,488
5Dec 25, 2025 01:2315,217224,912,659191,822,0005,994,0002,293,760
6Dec 25, 2025 01:2715,965239,342,237196,520,00011,029,0002,338,816
7Dec 25, 2025 01:2316,289241,153,271201,773,0009,988,0002,383,872
8Dec 25, 2025 01:2716,441241,914,431206,747,0006,991,0002,387,968
9Dec 25, 2025 01:2716,461244,105,461204,989,0008,999,0002,383,872
10Dec 25, 2025 01:2716,956244,199,834213,417,0007,013,0002,322,432
11Dec 25, 2025 01:2617,304253,092,686215,959,0008,998,0002,293,760
12Dec 25, 2025 01:2717,808258,155,262223,481,0008,017,0002,351,104