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 29, 2025 15:01Josu San MartinJosu San MartinScore: 3,624Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 14:583,13068,825,10338,659,0002,034,0002,416,640
2Dec 29, 2025 14:553,18063,707,29637,303,0004,032,0002,400,256
3Dec 29, 2025 14:583,29971,442,13841,885,000997,0002,404,352
4Dec 29, 2025 14:583,29973,181,05938,893,0003,989,0002,519,040
5Dec 29, 2025 14:573,37568,798,00540,881,0002,991,0002,490,368
6Dec 29, 2025 14:583,44175,687,99441,686,0003,050,0002,433,024
7Dec 29, 2025 14:583,46176,445,48841,988,0002,999,0002,269,184
8Dec 29, 2025 14:583,50567,723,83239,487,0006,074,0002,506,752
9Dec 29, 2025 14:573,51876,932,15840,651,0005,081,0002,285,568
10Dec 29, 2025 14:543,54971,514,68943,130,0003,009,0002,408,448
11Dec 29, 2025 14:573,56678,635,40243,332,0003,023,0002,330,624
12Dec 29, 2025 14:543,59374,461,23345,691,0001,015,0002,428,928
13Dec 29, 2025 14:553,60683,794,38244,883,0001,994,0002,449,408
14Dec 29, 2025 15:003,60778,962,46144,893,0001,995,0002,355,200
15Dec 29, 2025 14:523,61272,792,05942,960,0003,996,0002,351,104
16Dec 29, 2025 14:553,62474,053,80544,110,0003,007,0002,510,848
17Dec 29, 2025 14:583,62774,184,47243,133,0004,012,0002,420,736
18Dec 29, 2025 14:503,62874,406,51744,155,0003,010,0002,396,160
19Dec 29, 2025 14:583,63472,898,49041,216,0006,031,0002,363,392
20Dec 29, 2025 15:013,71477,092,84545,259,0003,017,0002,510,848
21Dec 29, 2025 15:003,72073,793,77246,341,0002,014,0002,379,776
22Dec 29, 2025 14:583,72079,531,98342,316,0006,045,0002,469,888
23Dec 29, 2025 15:003,82381,128,33047,677,0002,028,0002,330,624
24Dec 29, 2025 14:523,82579,892,19847,699,0002,029,0002,367,488
25Dec 29, 2025 14:503,83775,951,11145,892,0003,990,0002,379,776
26Dec 29, 2025 14:523,86177,245,16646,179,0004,015,0002,494,464
27Dec 29, 2025 14:503,86681,562,78948,249,0002,010,0002,412,544
28Dec 29, 2025 15:013,92885,239,28148,061,0003,003,0002,301,952
29Dec 29, 2025 15:013,93577,240,44846,135,0005,014,0002,412,544
30Dec 29, 2025 14:543,99976,898,29548,983,0002,998,0002,387,968