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 28, 2026 08:25Victor MerckléVictor MerckléScore: 400,897Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 28, 2026 08:26353,4994,615,829,3764,591,496,0003,997,0002,514,944stderr
2Apr 28, 2026 08:26366,8564,794,024,9344,761,136,0007,995,0002,535,424stderr
3Apr 28, 2026 08:26378,3434,940,093,5414,906,474,00011,988,0002,453,504stderr
4Apr 28, 2026 08:26379,5034,953,474,2504,924,558,0008,993,0002,527,232stderr
5Apr 28, 2026 08:26400,8975,234,427,8185,204,669,0006,994,0002,560,000stderr
6Apr 28, 2026 08:26406,2705,301,877,0885,273,519,0007,993,0002,420,736stderr
7Apr 28, 2026 08:26420,5675,495,829,7935,461,379,0005,994,0002,453,504stderr
8Apr 28, 2026 08:26433,0825,651,986,1815,620,082,0009,993,0002,404,352stderr
9Apr 28, 2026 08:26437,8285,716,695,7355,682,780,0008,993,0002,433,024stderr