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 13, 2026 23:30olliecrowolliecrowScore: 5,988Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 13, 2026 23:165,04495,693,99959,517,0006,052,0002,502,656
2Mar 13, 2026 23:305,29594,669,12760,857,0007,981,0002,412,544
3Mar 13, 2026 23:305,394101,058,85462,105,0008,013,0002,408,448
4Mar 13, 2026 23:165,564100,257,66268,313,0004,018,0002,375,680
5Mar 13, 2026 23:165,741104,114,17069,594,0005,043,0002,301,952
6Mar 13, 2026 23:305,760100,551,19371,850,0003,035,0002,277,376
7Mar 13, 2026 23:305,839107,896,74169,911,0005,992,0002,383,872
8Mar 13, 2026 23:165,853101,627,35571,083,0005,005,0002,449,408
9Mar 13, 2026 23:165,914105,740,04570,893,0005,991,0002,519,040
10Mar 13, 2026 23:165,988103,355,81374,856,0002,994,0002,392,064
11Mar 13, 2026 23:306,089106,272,78473,151,0006,012,0002,473,984
12Mar 13, 2026 23:306,231106,527,02675,006,0006,000,0002,342,912
13Mar 13, 2026 23:166,368109,175,18978,798,0003,989,0002,400,256
14Mar 13, 2026 23:306,375110,401,64276,886,0005,991,0002,519,040
15Mar 13, 2026 23:306,406112,598,00080,265,0003,009,0002,322,432
16Mar 13, 2026 23:166,545113,544,36280,076,0005,004,0002,420,736
17Mar 13, 2026 23:166,705114,818,03482,156,0005,009,0002,306,048
18Mar 13, 2026 23:307,188126,655,98688,422,0005,023,0002,494,464