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 listApr 19, 2026 05:47Charles CabergsCharles CabergsScore: 71,122Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 19, 2026 01:3161,758829,564,875791,854,00010,997,0002,387,968
2Apr 19, 2026 05:4762,087832,786,799799,133,0008,001,0002,498,560
3Apr 19, 2026 05:4764,401861,724,328829,208,0008,002,0002,433,024
4Apr 19, 2026 01:3164,998877,166,473836,980,0007,999,0002,383,872
5Apr 19, 2026 01:3165,053870,226,769836,692,0008,996,0002,293,760
6Apr 19, 2026 01:3166,874895,136,501858,368,00010,991,0002,473,984
7Apr 19, 2026 05:4767,740906,683,969876,624,0003,998,0002,490,368
8Apr 19, 2026 01:3168,188913,389,042876,457,0009,993,0002,383,872
9Apr 19, 2026 05:4768,719922,343,316886,347,0006,994,0002,502,656
10Apr 19, 2026 01:3171,122949,752,265917,592,0006,996,0002,523,136
11Apr 19, 2026 05:4771,463956,217,771917,025,00012,000,0002,527,232
12Apr 19, 2026 05:4773,212977,480,720941,761,0009,997,0002,465,792
13Apr 19, 2026 05:4773,327978,500,612945,260,0007,993,0002,392,064
14Apr 19, 2026 05:4773,995990,870,346950,934,00010,999,0002,502,656
15Apr 19, 2026 05:4777,2931,030,526,602996,808,0007,998,0002,387,968
16Apr 19, 2026 01:3181,3651,083,120,4971,047,754,0009,997,0002,351,104
17Apr 19, 2026 01:3184,2211,121,211,4601,084,877,0009,998,0002,330,624
18Apr 19, 2026 01:3189,1081,183,436,5081,146,411,00011,993,0002,289,664