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 listMar 20, 2026 19:57Yuriy LyfenkoYuriy LyfenkoScore: 2,434Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 20, 2026 19:562,19758,341,88922,438,0006,119,0002,338,816
2Mar 20, 2026 19:562,35355,109,64125,490,0005,098,0002,363,392
3Mar 20, 2026 19:562,37565,113,50127,887,0002,987,0002,293,760
4Mar 20, 2026 19:562,37752,473,41023,922,0006,977,0002,342,912
5Mar 20, 2026 19:562,37759,619,50624,924,0005,981,0002,490,368
6Mar 20, 2026 19:562,37950,173,85924,937,0005,984,0002,420,736
7Mar 20, 2026 19:562,39856,770,05625,144,0006,034,0002,363,392
8Mar 20, 2026 19:562,43456,188,18827,556,0004,082,0002,527,232
9Mar 20, 2026 19:562,46260,575,55127,006,0005,001,0002,453,504
10Mar 20, 2026 19:562,60457,790,82129,870,0003,982,0002,490,368
11Mar 20, 2026 19:562,62762,349,50029,130,0005,022,0002,453,504
12Mar 20, 2026 19:562,69057,762,63628,973,0005,994,0002,428,928
13Mar 20, 2026 19:572,71160,106,98829,200,0006,041,0002,383,872
14Mar 20, 2026 19:572,73966,205,03931,536,0004,069,0002,338,816
15Mar 20, 2026 19:572,79062,719,46831,229,0005,037,0002,412,544