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 30, 2025 05:32Josu San MartinJosu San MartinScore: 3,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 30, 2025 05:273,03168,022,29037,382,0002,020,0002,453,504
2Dec 30, 2025 05:323,04462,462,56538,561,0001,014,0002,465,792
3Dec 30, 2025 05:273,17069,584,51338,190,0003,015,0002,334,720
4Dec 30, 2025 05:483,26769,654,92536,409,0006,068,0002,342,912
5Dec 30, 2025 05:273,26863,376,21438,436,0004,045,0002,396,160
6Dec 30, 2025 05:313,34966,243,16541,510,0002,024,0002,473,984
7Dec 30, 2025 05:313,35572,917,59537,531,0006,086,0002,375,680
8Dec 30, 2025 05:323,35974,303,12340,617,0003,046,0002,478,080
9Dec 30, 2025 05:273,40970,891,17841,292,0003,021,0002,355,200
10Dec 30, 2025 05:323,48374,849,91142,256,0003,018,0002,306,048
11Dec 30, 2025 05:323,50874,933,27044,593,0001,013,0002,379,776
12Dec 30, 2025 05:273,53071,739,94243,893,0001,995,0002,367,488
13Dec 30, 2025 05:483,56671,861,54342,333,0004,031,0002,342,912
14Dec 30, 2025 05:383,56969,280,73943,366,0003,025,0002,420,736
15Dec 30, 2025 05:383,58075,885,96842,490,0004,046,0002,260,992
16Dec 30, 2025 05:383,60575,798,80045,863,000997,0002,510,848
17Dec 30, 2025 05:313,64875,180,49143,392,0004,036,0002,314,240
18Dec 30, 2025 05:383,67974,065,24646,831,000996,0002,355,200
19Dec 30, 2025 05:483,68779,203,00843,938,0003,994,0002,473,984
20Dec 30, 2025 05:273,68874,544,93443,955,0003,995,0002,387,968
21Dec 30, 2025 05:273,70383,459,20046,137,0002,005,0002,428,928
22Dec 30, 2025 05:383,71974,635,25944,315,0004,028,0002,314,240
23Dec 30, 2025 05:323,73072,632,71145,458,0003,030,0002,449,408
24Dec 30, 2025 05:383,76981,834,89346,001,0003,000,0002,482,176
25Dec 30, 2025 05:323,83676,627,79246,873,0002,991,0002,297,856
26Dec 30, 2025 05:323,84575,835,59046,990,0002,999,0002,449,408
27Dec 30, 2025 05:273,95272,986,03749,366,0002,014,0002,256,896
28Dec 30, 2025 05:324,02778,241,25650,337,0002,013,0002,301,952
29Dec 30, 2025 05:324,04278,912,56348,508,0004,042,0002,473,984
30Dec 30, 2025 05:274,09083,881,87252,173,0001,003,0002,363,392