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 listOct 16, 2026 22:00janwwwjanwwwError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 25, 2026 17:37Andrew Epstein400.37CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 25, 2026 17:40054,690,806,14254,671,610,0007,676,0006,311,936line 136635: expected liquidate 21056 -2396 2396000100 got liquidate 99999 100000000028548259 8500544
2Oct 25, 2026 17:40054,588,502,61954,569,585,0007,430,0006,311,936line 136497: expected liquidate 21017 -62940 1000099 got liquidate 99999 100000000000293379 8500544
3Oct 25, 2026 17:40054,609,969,91654,588,340,0009,341,0006,311,936line 136466: expected liquidate 21110 0 7724146491 got liquidate 99999 100000000013926339 8500544
4Oct 16, 2026 22:012,27740,217,05127,417,0002,195,0002,117,632
5Oct 16, 2026 22:012,30840,703,80927,982,0002,027,0002,117,632
6Oct 16, 2026 22:012,41441,955,55629,361,0002,030,0002,117,632
7Oct 16, 2026 22:012,43542,347,34829,624,0002,043,0002,117,632
8Oct 16, 2026 22:012,47842,777,08830,189,0002,027,0002,117,632
9Oct 16, 2026 22:012,47942,940,75730,199,0002,040,0002,117,632
10Oct 16, 2026 22:012,48342,878,43230,252,0002,028,0002,117,632
11Oct 16, 2026 22:012,51643,346,78030,571,0002,138,0002,117,632
12Oct 16, 2026 22:012,52243,473,61730,516,0002,275,0002,117,632