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 16:05Josu San MartinJosu San MartinScore: 15,216Success
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 16:0312,653189,145,780158,476,0006,018,0002,351,104
2Dec 25, 2025 16:0313,511209,682,024165,665,0009,979,0002,306,048
3Dec 25, 2025 16:0314,164214,003,982175,127,0009,006,0002,441,216
4Dec 25, 2025 16:0514,194216,336,879174,496,00010,028,0002,334,720
5Dec 25, 2025 16:0314,238216,486,448176,092,0009,004,0002,523,136
6Dec 25, 2025 16:0314,292214,068,926174,814,00010,988,0002,510,848
7Dec 25, 2025 16:0514,408213,918,876181,301,0006,009,0002,293,760
8Dec 25, 2025 16:0514,763222,299,419183,920,0007,996,0002,478,080
9Dec 25, 2025 16:0615,213230,317,481189,775,0007,990,0002,490,368
10Dec 25, 2025 16:0315,216228,294,007189,810,0007,992,0002,416,640
11Dec 25, 2025 16:0315,237221,178,338186,082,00012,005,0002,301,952
12Dec 25, 2025 16:0615,431236,734,116191,572,0009,026,0002,441,216
13Dec 25, 2025 16:0315,445232,159,392189,747,00011,043,0002,510,848
14Dec 25, 2025 16:0615,959238,039,121200,448,0007,015,0002,494,464
15Dec 25, 2025 16:0516,135237,684,076198,768,00010,987,0002,510,848
16Dec 25, 2025 16:0516,290242,377,005202,782,0008,990,0002,514,944
17Dec 25, 2025 16:0517,101245,792,809213,301,0009,012,0002,510,848
18Dec 25, 2025 16:0317,942260,151,271224,232,0009,009,0002,269,184