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 listDec 25, 2025 01:08Josu San MartinJosu San MartinScore: 14,940Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 01:0811,535177,568,785143,952,0005,998,0002,297,856
2Dec 25, 2025 01:0813,157198,261,556164,037,0007,001,0002,510,848
3Dec 25, 2025 01:1013,622208,902,220170,077,0007,003,0002,404,352
4Dec 25, 2025 01:0814,063208,906,986174,824,0007,991,0002,461,696
5Dec 25, 2025 01:0814,093205,702,204177,203,0006,006,0002,531,328
6Dec 25, 2025 01:1014,308220,252,589180,999,0004,999,0002,359,296
7Dec 25, 2025 01:1014,408215,456,756178,288,0009,014,0002,482,176
8Dec 25, 2025 01:0714,466212,939,413181,062,0007,002,0002,408,448
9Dec 25, 2025 01:1014,571217,775,286184,413,0005,011,0002,359,296
10Dec 25, 2025 01:0714,940220,390,383187,217,0007,008,0002,297,856
11Dec 25, 2025 01:0815,014224,216,386187,170,0008,007,0002,523,136
12Dec 25, 2025 01:0715,442228,216,309193,758,0006,991,0002,490,368
13Dec 25, 2025 01:0815,581232,675,848201,547,0001,002,0002,494,464
14Dec 25, 2025 01:0815,737236,240,670194,554,00010,028,0002,428,928
15Dec 25, 2025 01:0816,138237,874,707203,801,0005,994,0002,347,008
16Dec 25, 2025 01:1016,244239,048,351204,171,0007,005,0002,420,736
17Dec 25, 2025 01:1016,365239,486,168205,759,0006,991,0002,519,040
18Dec 25, 2025 01:0816,365238,385,037202,763,0009,988,0002,404,352