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 10, 2026 06:40janwwwjanwwwError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 14, 2026 22:09Andrew Epstein499.59CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 14, 2026 22:14061,234,766,02761,212,962,0009,641,0006,307,840line 7: expected liquidate 8 -1152921504613230017 246914 got liquidate 8 2 246914
2Oct 14, 2026 22:14060,737,923,71760,718,711,0007,636,0006,307,840line 7: expected liquidate 8 -1152921504635258625 246914 got liquidate 8 2 246914
3Oct 14, 2026 22:14061,364,301,24461,344,615,0007,827,0006,307,840line 7: expected liquidate 8 -1152921504623206945 246914 got liquidate 8 2 246914
4Oct 10, 2026 06:431,90135,274,12022,627,0002,091,0002,113,536
5Oct 10, 2026 06:431,94435,803,13323,148,0002,132,0002,113,536
6Oct 10, 2026 06:431,96136,004,34023,280,0002,215,0002,113,536
7Oct 10, 2026 06:431,97936,255,12623,425,0002,304,0002,113,536
8Oct 10, 2026 06:431,98936,453,81623,716,0002,147,0002,113,536
9Oct 10, 2026 06:432,01236,720,64224,001,0002,158,0002,113,536
10Oct 10, 2026 06:432,10037,859,63625,103,0002,207,0002,113,536
11Oct 10, 2026 06:432,11037,959,59925,245,0002,185,0002,113,536
12Oct 10, 2026 06:432,15738,656,93225,773,0002,276,0002,113,536