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:19Josu San MartinJosu San MartinScore: 5,605Success
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:484,78788,505,63661,224,0001,003,0002,269,184
2Dec 27, 2025 15:495,02689,430,78861,322,0004,021,0002,363,392
3Dec 27, 2025 15:485,09392,236,89261,193,0005,015,0002,502,656
4Dec 27, 2025 15:485,11599,027,35762,464,0004,029,0002,334,720
5Dec 27, 2025 15:495,13198,542,12661,656,0005,053,0002,519,040
6Dec 27, 2025 15:485,28092,016,07064,598,0004,037,0002,318,336
7Dec 27, 2025 15:495,31592,979,23265,095,0004,005,0002,498,560
8Dec 27, 2025 15:485,31798,824,60364,114,0005,008,0002,478,080
9Dec 27, 2025 15:495,35497,235,29466,577,0003,026,0002,473,984
10Dec 27, 2025 15:495,402100,068,47965,206,0005,015,0002,273,280
11Dec 27, 2025 15:495,437103,433,90065,637,0005,049,0002,494,464
12Dec 27, 2025 15:495,473101,134,37167,147,0004,008,0002,449,408
13Dec 27, 2025 15:495,49494,533,16569,412,0002,011,0002,473,984
14Dec 27, 2025 15:495,528105,436,29466,871,0004,990,0002,449,408
15Dec 27, 2025 15:505,57097,707,80268,386,0004,022,0002,420,736
16Dec 27, 2025 15:495,603100,782,91067,850,0004,989,0002,428,928
17Dec 27, 2025 15:205,605102,002,20465,883,0006,987,0002,383,872
18Dec 27, 2025 15:205,62296,324,98771,087,0002,002,0002,478,080
19Dec 27, 2025 15:485,797108,053,45472,344,0003,014,0002,519,040
20Dec 27, 2025 15:495,797105,204,68172,342,0003,014,0002,297,856
21Dec 27, 2025 15:505,84198,638,10672,938,0002,997,0002,469,888
22Dec 27, 2025 15:495,872107,981,75971,317,0005,022,0002,519,040
23Dec 27, 2025 15:195,879105,116,36174,417,0002,011,0002,289,664
24Dec 27, 2025 15:205,940105,106,40273,212,0004,011,0002,273,280
25Dec 27, 2025 15:505,964101,465,79972,499,0005,034,0002,359,296
26Dec 27, 2025 15:195,978107,980,16575,690,0002,018,0002,342,912
27Dec 27, 2025 15:485,980106,381,34273,701,0004,038,0002,445,312
28Dec 27, 2025 15:486,087107,047,24774,125,0005,008,0002,465,792
29Dec 27, 2025 15:196,373111,371,64280,829,0002,020,0002,461,696
30Dec 27, 2025 15:496,486113,014,07179,294,0005,018,0002,506,752
31Dec 27, 2025 15:486,542118,411,34782,043,0003,001,0002,473,984
32Dec 27, 2025 15:496,627115,287,96880,142,0006,010,0002,265,088
33Dec 27, 2025 15:496,744116,571,71083,636,0004,030,0002,269,184