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 listMar 15, 2026 21:33Dominique GarmierDominique GarmierScore: 39,232Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 15, 2026 11:1228,265397,625,503358,434,0009,010,0002,457,600
2Mar 15, 2026 21:3332,671448,920,979419,723,0004,996,0002,289,664
3Mar 15, 2026 10:0833,819470,122,739433,650,0005,995,0002,310,144
4Mar 15, 2026 10:3536,904506,897,606468,756,00010,994,0002,510,848
5Mar 15, 2026 10:0837,752521,993,901484,779,0005,997,0002,396,160
6Mar 15, 2026 14:0337,982515,459,119485,775,0007,996,0002,412,544
7Mar 15, 2026 14:0338,590533,733,309494,669,0006,995,0002,408,448
8Mar 15, 2026 10:3539,232539,258,183502,013,0008,000,0002,367,488
9Mar 15, 2026 11:1240,014549,012,781507,179,00013,004,0002,416,640
10Mar 15, 2026 21:3340,970559,850,343523,623,0008,993,0002,326,528
11Mar 15, 2026 11:1241,507561,991,669532,602,0006,994,0002,506,752
12Mar 15, 2026 10:3542,301570,939,822540,919,0008,998,0002,449,408
13Mar 15, 2026 10:0843,045584,373,769550,594,0008,993,0002,449,408
14Mar 15, 2026 21:3345,108619,067,399578,413,0007,991,0002,396,160
15Mar 15, 2026 14:0345,280613,421,304580,646,0007,995,0002,469,888