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 14, 2026 01:22Karthik ReddyKarthik ReddyError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Nov 2, 2026 14:09Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 2, 2026 14:12034,753,665,64234,551,521,000149,351,0005,763,072line 3: expected liquidate 5 20047 9900000 got liquidate 4 0 10000000000
2Nov 2, 2026 14:12034,962,169,00534,762,297,000143,163,0005,996,544line 3: expected liquidate 5 20047 9900000 got liquidate 4 0 10000000000
3Nov 2, 2026 14:12035,109,911,11934,915,816,000143,859,0006,352,896line 3: expected liquidate 5 20047 9900000 got liquidate 4 0 10000000000
4Oct 14, 2026 01:221,40227,211,12417,125,0001,101,00073,728
5Oct 14, 2026 01:221,47928,135,19218,141,0001,095,00077,824
6Oct 14, 2026 01:221,48228,179,81218,182,0001,087,00077,824
7Oct 14, 2026 01:221,51028,585,90818,544,0001,091,00081,920
8Oct 14, 2026 01:221,59729,712,24919,685,0001,087,00090,112
9Oct 14, 2026 01:221,67630,720,53120,749,0001,039,00077,824
10Oct 14, 2026 01:221,71031,215,22221,177,0001,055,00086,016
11Oct 14, 2026 01:221,74131,722,32921,604,0001,035,00098,304
12Oct 14, 2026 01:221,80732,403,44522,436,0001,058,00090,112