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 listApr 30, 2026 11:27Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 4, 2026 14:17Tomislav Tunkovic303.30CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 4, 2026 14:41016,513,78406,454,0002,121,728line 1: expected liquidate 1 -27671 531846 got liquidate 5 21499 15393672
2Sep 4, 2026 14:41016,461,74906,438,0002,121,728line 2: expected liquidate 5 -131365 6561529 got liquidate 8 -244786 15163470
3Sep 4, 2026 14:41017,344,86506,644,0002,121,728line 11: expected liquidate 2 -299015 7379993 got liquidate 1 -653698 2804438
4Apr 30, 2026 11:282,80956,300,82928,406,0008,116,0002,445,312
5Apr 30, 2026 11:282,95054,829,96429,271,0009,084,0002,359,296
6Apr 30, 2026 11:282,95254,078,05528,284,00010,101,0002,355,200
7Apr 30, 2026 11:283,04358,283,45533,481,0006,087,0002,437,120
8Apr 30, 2026 11:283,05456,105,89232,579,0007,126,0002,498,560
9Apr 30, 2026 11:283,06360,001,57133,852,0005,973,0002,461,696
10Apr 30, 2026 11:283,13858,005,41529,856,00010,947,0002,379,776
11Apr 30, 2026 11:283,26860,987,58434,401,0008,094,0002,392,064
12Apr 30, 2026 11:283,28558,963,43332,538,00010,168,0002,416,640