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 19:24Josu San MartinJosu San MartinScore: 12,907Success
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 19:2511,488182,165,534148,344,0001,002,0002,281,472
2Dec 25, 2025 19:2511,687187,738,891147,930,0003,998,0002,297,856
3Dec 25, 2025 19:2512,036179,130,729152,462,0004,012,0002,281,472
4Dec 25, 2025 19:2412,307189,938,829155,992,0003,999,0002,379,776
5Dec 25, 2025 19:2312,367190,805,021158,775,0001,997,0002,281,472
6Dec 25, 2025 19:2512,481192,548,985160,245,0002,003,0002,486,272
7Dec 25, 2025 19:2512,545194,129,331159,089,0004,002,0002,326,528
8Dec 25, 2025 19:2512,580192,897,893158,525,0005,016,0002,297,856
9Dec 25, 2025 19:2512,598194,021,938157,788,0005,991,0002,273,280
10Dec 25, 2025 19:2312,622193,228,468158,088,0006,003,0002,351,104
11Dec 25, 2025 19:2112,907194,589,088163,797,0003,995,0002,347,008
12Dec 25, 2025 19:2413,040200,028,689166,508,0003,009,0002,322,432
13Dec 25, 2025 19:2413,104198,058,040168,343,0002,004,0002,281,472
14Dec 25, 2025 19:2513,379196,439,424171,926,0001,999,0002,301,952
15Dec 25, 2025 19:2113,510201,526,025170,611,0005,017,0002,424,832
16Dec 25, 2025 19:2414,004214,489,513178,054,0004,001,0002,433,024
17Dec 25, 2025 19:2414,675231,329,017188,776,0001,997,0002,363,392
18Dec 25, 2025 19:2514,857218,755,926186,134,0007,005,0002,379,776
19Dec 25, 2025 19:2314,883225,364,740188,462,0005,012,0002,514,944
20Dec 25, 2025 19:2115,598232,193,206197,783,0004,994,0002,514,944
21Dec 25, 2025 19:2416,358238,118,955205,668,0006,988,0002,322,432