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 11, 2025 21:19Ludvig SjöströmLudvig SjöströmScore: 464,505Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 11, 2025 21:19383,7175,020,910,4734,980,333,0007,994,0002,437,120
2Oct 11, 2025 21:19385,2535,043,341,0915,006,296,0001,998,0002,469,888
3Oct 11, 2025 21:19405,8675,303,367,7875,273,272,0002,997,0002,478,080
4Oct 11, 2025 21:19416,5195,448,761,6035,402,755,00011,990,0002,260,992
5Oct 11, 2025 21:19464,5056,073,828,8636,029,573,0008,993,0002,375,680
6Oct 11, 2025 21:19492,5916,435,835,6806,397,693,0005,995,0002,301,952
7Oct 11, 2025 21:19505,1406,599,064,8136,558,834,0007,992,0002,281,472
8Oct 11, 2025 21:19518,0136,765,473,8516,726,178,0007,994,0002,297,856
9Oct 11, 2025 21:19530,7256,929,126,9256,892,434,0006,994,0002,502,656