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 27, 2025 15:24Josu San MartinJosu San MartinScore: 5,641Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 27, 2025 15:444,42286,382,24855,471,0002,017,0002,318,336
2Dec 27, 2025 15:444,61685,510,37556,013,0004,000,0002,392,064
3Dec 27, 2025 15:444,63191,739,52354,186,0006,020,0002,478,080
4Dec 27, 2025 15:444,81691,057,58659,582,0003,029,0002,404,352
5Dec 27, 2025 15:245,03894,058,71662,470,0003,022,0002,367,488
6Dec 27, 2025 15:455,08990,775,88961,141,0005,011,0002,412,544
7Dec 27, 2025 15:235,10596,395,98363,343,0003,016,0002,396,160
8Dec 27, 2025 15:235,14696,738,70463,909,0002,995,0002,301,952
9Dec 27, 2025 15:445,14993,020,95960,948,0005,994,0002,306,048
10Dec 27, 2025 15:445,24699,622,47963,187,0005,014,0002,351,104
11Dec 27, 2025 15:235,39895,150,29064,165,0006,015,0002,449,408
12Dec 27, 2025 15:445,401103,422,80968,206,0002,006,0002,351,104
13Dec 27, 2025 15:455,557103,713,30567,230,0005,017,0002,347,008
14Dec 27, 2025 15:235,641102,137,76270,315,0003,013,0002,367,488
15Dec 27, 2025 15:445,644105,424,64869,352,0004,020,0002,310,144
16Dec 27, 2025 15:235,72498,545,83767,376,0007,039,0002,338,816
17Dec 27, 2025 15:235,758103,813,79669,868,0004,990,0002,449,408
18Dec 27, 2025 15:235,797101,277,65472,353,0003,014,0002,367,488
19Dec 27, 2025 15:235,822103,654,10674,678,0001,009,0002,379,776
20Dec 27, 2025 15:235,86399,123,19469,197,0007,020,0002,461,696
21Dec 27, 2025 15:235,920101,521,09273,962,0002,998,0002,314,240
22Dec 27, 2025 15:445,960106,194,30575,474,0002,012,0002,297,856
23Dec 27, 2025 15:456,264109,534,81377,409,0004,021,0002,506,752
24Dec 27, 2025 15:246,341107,650,33980,428,0002,010,0002,375,680
25Dec 27, 2025 15:236,460114,164,47481,975,0001,999,0002,330,624
26Dec 27, 2025 15:236,488117,389,06081,338,0003,012,0002,338,816
27Dec 27, 2025 15:246,824115,236,47987,701,0001,008,0002,363,392