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 listMay 16, 2026 19:40M-RochatM-RochatError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1May 16, 2026 19:40011,875,4671,416,00002,121,728line 0: expected liquidate 4545 45840 4723950 got prefix bytes=7686273 data=a 762925|a 783090|p 0 12971|a 830118|a 1462968|a 1251678|a 1079225|a 949898|a 1075740|a 1050183|a 1246634|p 1 7093|a 826629|a 805687|a 744194|a 839879|a 835619|a 1338367|p 2 57...
2May 16, 2026 19:40010,184,9501,391,00002,121,728line 0: expected liquidate 2583 -361731 16675240 got prefix bytes=7750296 data=p 0 18045|a 1228249|a 876728|p 1 9038|a 786774|p 2 9578|a 896886|a 1212763|p 3 15178|a 1059390|a 887601|a 1087927|p 4 11484|a 1072712|a 896123|a 1122289|p 5 5953|a 1114772|a...
3May 16, 2026 19:40010,161,8701,396,00002,121,728line 0: expected liquidate 3232 71539 7387460 got prefix bytes=7817462 data=a 1195971|a 1292843|a 929402|p 0 12668|a 748394|a 1290565|a 1140048|p 1 11410|p 2 14888|p 3 11147|a 945659|a 1108894|a 1099825|p 4 1000|a 945462|p 5 12010|a 896828|a 241886|p 6...