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 04:24Josu San MartinJosu San MartinScore: 22,214Success
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 04:2419,377274,634,870247,900,0003,998,0002,482,176
2Dec 25, 2025 04:0320,207290,121,612256,695,0005,992,0002,383,872
3Dec 25, 2025 04:0421,269303,221,912273,490,0003,005,0002,437,120
4Dec 25, 2025 04:1021,847314,213,958275,007,0009,000,0002,326,528
5Dec 25, 2025 04:1021,939314,922,510276,199,0009,006,0002,469,888
6Dec 25, 2025 04:0322,130319,259,195282,696,0004,994,0002,412,544
7Dec 25, 2025 04:0322,201313,173,268282,621,0005,991,0002,297,856
8Dec 25, 2025 04:0422,214321,884,999284,781,0003,996,0002,281,472
9Dec 25, 2025 04:0422,334310,237,339284,340,0006,007,0002,297,856
10Dec 25, 2025 04:0322,669321,086,062286,705,0007,991,0002,334,720
11Dec 25, 2025 04:2424,017339,847,857306,222,0006,004,0002,306,048
12Dec 25, 2025 04:0324,052340,361,644310,675,0001,997,0002,482,176
13Dec 25, 2025 04:1025,094351,008,243322,207,0004,015,0002,375,680
14Dec 25, 2025 04:0325,246354,363,903323,198,0005,003,0002,506,752
15Dec 25, 2025 04:2426,841377,530,272341,940,0006,998,0002,375,680