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 26, 2025 22:34Josu San MartinJosu San MartinScore: 7,279Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 22:255,888110,007,06969,493,0007,050,0002,301,952
2Dec 26, 2025 22:256,031102,336,33071,372,0007,036,0002,322,432
3Dec 26, 2025 22:256,335113,687,56077,338,0005,021,0002,338,816
4Dec 26, 2025 22:246,435112,273,20282,648,0001,007,0002,420,736
5Dec 26, 2025 22:246,450115,563,56777,860,0005,989,0002,437,120
6Dec 26, 2025 22:256,793115,010,63481,283,0007,024,0002,478,080
7Dec 26, 2025 22:257,065122,072,92387,846,0003,993,0002,404,352
8Dec 26, 2025 22:257,169124,013,75888,181,0005,010,0002,433,024
9Dec 26, 2025 22:257,202123,148,54290,603,0003,020,0002,379,776
10Dec 26, 2025 22:257,216117,945,42991,815,0001,995,0002,297,856
11Dec 26, 2025 22:257,279122,349,39290,598,0004,026,0002,318,336
12Dec 26, 2025 22:257,346118,403,54089,472,0006,031,0002,412,544
13Dec 26, 2025 22:257,358124,629,98394,643,0001,006,0002,342,912
14Dec 26, 2025 22:257,363122,988,17190,735,0004,985,0002,383,872
15Dec 26, 2025 22:347,511131,643,09992,609,0005,033,0002,469,888
16Dec 26, 2025 22:257,522125,625,44593,800,0003,991,0002,297,856
17Dec 26, 2025 22:347,754127,155,92896,810,0003,992,0002,281,472
18Dec 26, 2025 22:257,763130,505,25394,925,0005,995,0002,428,928
19Dec 26, 2025 22:347,892136,951,59998,572,0004,023,0002,404,352
20Dec 26, 2025 22:248,275135,102,474104,556,0003,016,0002,408,448
21Dec 26, 2025 22:258,808136,524,610109,480,0005,022,0002,375,680