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 22:04Josu San MartinJosu San MartinScore: 13,265Success
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 22:0411,267176,942,926142,456,0004,012,0002,445,312
2Dec 25, 2025 22:0412,446188,609,363157,799,0003,994,0002,371,584
3Dec 25, 2025 22:0412,643192,749,333161,353,0003,006,0002,420,736
4Dec 25, 2025 22:0412,959189,705,273164,451,0004,011,0002,473,984
5Dec 25, 2025 22:0413,039198,271,147167,507,0002,006,0002,445,312
6Dec 25, 2025 22:0413,048198,734,620164,605,0005,018,0002,367,488
7Dec 25, 2025 22:0413,076193,385,875166,989,0002,999,0002,371,584
8Dec 25, 2025 22:0413,127201,322,823166,656,0003,991,0002,400,256
9Dec 25, 2025 22:0413,215194,598,973166,796,0004,993,0002,351,104
10Dec 25, 2025 22:0413,265199,072,124170,446,0002,005,0002,416,640
11Dec 25, 2025 22:0413,291201,403,236166,793,0005,992,0002,334,720
12Dec 25, 2025 22:0413,500209,411,263173,498,0002,005,0002,473,984
13Dec 25, 2025 22:0413,795205,521,072177,326,0002,003,0002,461,696
14Dec 25, 2025 22:0414,074214,034,197181,961,000999,0002,277,376
15Dec 25, 2025 22:0414,541220,347,351186,027,0003,000,0002,301,952
16Dec 25, 2025 22:0414,664222,123,554188,632,0002,006,0002,482,176
17Dec 25, 2025 22:0415,366227,201,609195,767,0003,995,0002,289,664
18Dec 25, 2025 22:0416,872250,616,371213,324,0006,009,0002,293,760