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 listMay 5, 2026 12:47Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 4, 2026 13:37Tomislav Tunkovic10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 4, 2026 14:16011,613,57202,030,0002,187,264line 3: expected liquidate 3 -3262 6116716 got liquidate 4 118206 20627251
2Sep 4, 2026 14:16011,448,10201,991,0002,187,264line 3: expected liquidate 8 -280348 9201330 got liquidate 2 83295 14852663
3Sep 4, 2026 14:16011,522,23202,049,0002,187,264line 3: expected liquidate 4 -365165 7518220 got liquidate 8 -227721 18316581
4May 5, 2026 12:581,70430,192,40319,719,0002,434,0002,498,560
5May 5, 2026 12:581,76030,864,09220,593,0002,290,0002,498,560
6May 5, 2026 12:581,77533,282,13920,781,0002,296,0002,469,888
7May 5, 2026 12:581,87132,321,40121,912,0002,417,0002,408,448
8May 5, 2026 12:581,91532,958,49322,469,0002,432,0002,363,392
9May 5, 2026 12:581,93333,135,65322,677,0002,459,0002,355,200
10May 5, 2026 12:581,97933,805,13323,301,0002,429,0002,592,768
11May 5, 2026 12:582,04134,196,01324,030,0002,515,0002,519,040
12May 5, 2026 12:582,05634,779,74224,317,0002,420,0002,551,808