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 12, 2025 11:50Enda CarrollEnda CarrollScore: 233,148Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 12, 2025 11:50174,6702,294,647,4462,260,712,0009,994,0002,510,848
2Oct 12, 2025 11:50194,1792,556,356,8442,514,336,0009,993,0002,420,736
3Oct 12, 2025 11:50198,9772,619,635,9782,578,703,0007,995,0002,433,024
4Oct 12, 2025 11:50201,8172,649,994,4032,615,629,0007,998,0002,412,544
5Oct 12, 2025 11:50233,1483,060,465,8043,019,937,00010,992,0002,347,008
6Oct 12, 2025 11:50244,0223,205,172,0423,163,294,0008,995,0002,322,432
7Oct 12, 2025 11:50254,4323,335,874,7113,297,623,0009,995,0002,383,872
8Oct 12, 2025 11:50256,0913,357,723,3183,315,187,00013,992,0002,408,448
9Oct 12, 2025 11:50263,9913,460,621,6953,421,885,0009,993,0002,457,600