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 5, 2025 18:54Yuriy LyfenkoYuriy LyfenkoScore: 177,354Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2025 18:54140,6301,857,639,4251,818,192,0009,995,0002,441,216
2Oct 5, 2025 18:54141,8571,871,066,9071,833,151,00010,994,0002,428,928
3Oct 5, 2025 18:54143,4571,898,411,7071,856,951,0007,995,0002,383,872
4Oct 5, 2025 18:54148,3721,954,383,0841,918,837,0009,993,0002,383,872
5Oct 5, 2025 18:54177,3542,338,172,8642,295,609,0009,993,0002,404,352
6Oct 5, 2025 18:54189,4592,488,586,8542,456,969,0005,997,0002,433,024
7Oct 5, 2025 18:54190,8662,510,949,3562,476,268,0004,996,0002,514,944
8Oct 5, 2025 18:54193,5382,549,805,6652,509,003,0006,997,0002,342,912
9Oct 5, 2025 18:54199,3562,626,141,1342,585,627,0005,996,0002,441,216