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 listFeb 19, 2026 05:17Josu San MartinJosu San MartinScore: 3,638Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 19, 2026 05:193,26962,753,42439,459,0003,035,0002,437,120
2Feb 19, 2026 05:043,27873,244,16338,552,0004,058,0002,416,640
3Feb 19, 2026 05:153,40875,360,72042,286,0002,013,0002,473,984
4Feb 19, 2026 05:153,41374,427,44242,352,0002,016,0002,482,176
5Feb 19, 2026 05:193,55172,570,19843,147,0003,010,0002,514,944
6Feb 19, 2026 05:173,58467,314,83443,557,0003,038,0002,383,872
7Feb 19, 2026 05:043,63880,641,22344,280,0003,019,0002,289,664
8Feb 19, 2026 05:173,63975,549,03843,276,0004,025,0002,445,312
9Feb 19, 2026 05:193,64970,316,52743,402,0004,037,0002,482,176
10Feb 19, 2026 05:043,80370,552,35145,407,0004,036,0002,486,272
11Feb 19, 2026 05:173,84377,533,37945,961,0003,996,0002,330,624
12Feb 19, 2026 05:154,14482,359,17650,881,0002,993,0002,428,928