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 04:48Josu San MartinJosu San MartinScore: 3,459Success
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 04:382,99266,191,93635,901,0002,991,0002,408,448
2Dec 30, 2025 04:453,11263,247,46236,414,0004,046,0002,461,696
3Dec 30, 2025 04:383,12575,359,48438,598,0002,031,0002,330,624
4Dec 30, 2025 04:383,14670,658,72833,912,0006,981,0002,510,848
5Dec 30, 2025 04:453,23069,594,54938,996,0002,999,0002,506,752
6Dec 30, 2025 04:453,30267,968,05639,936,0002,995,0002,383,872
7Dec 30, 2025 04:383,30674,661,04940,981,0001,999,0002,330,624
8Dec 30, 2025 04:453,32269,352,40941,179,0002,008,0002,441,216
9Dec 30, 2025 04:453,34971,785,74242,525,0001,012,0002,445,312
10Dec 30, 2025 04:453,35867,075,08540,608,0003,045,0002,404,352
11Dec 30, 2025 04:453,38370,019,63240,975,0002,998,0002,416,640
12Dec 30, 2025 04:483,45274,816,39342,881,0001,994,0002,269,184
13Dec 30, 2025 04:443,45672,897,81541,928,0002,994,0002,457,600
14Dec 30, 2025 04:383,45976,232,80242,963,0001,998,0002,367,488
15Dec 30, 2025 04:383,46972,724,84943,090,0002,004,0002,424,832
16Dec 30, 2025 04:443,52874,173,00342,879,0002,991,0002,428,928
17Dec 30, 2025 04:443,52881,736,03042,875,0002,991,0002,293,760
18Dec 30, 2025 04:383,54272,058,24042,047,0004,004,0002,478,080
19Dec 30, 2025 04:483,56869,553,25041,337,0005,041,0002,445,312
20Dec 30, 2025 04:383,57171,252,97042,381,0004,036,0002,326,528
21Dec 30, 2025 04:483,60676,823,30745,875,000997,0002,379,776
22Dec 30, 2025 04:453,62277,952,77843,080,0004,007,0002,375,680
23Dec 30, 2025 04:453,68273,559,67645,878,0001,994,0002,297,856
24Dec 30, 2025 04:383,68275,966,52842,882,0004,986,0002,424,832
25Dec 30, 2025 04:453,71772,093,51143,288,0005,033,0002,367,488
26Dec 30, 2025 04:453,74774,087,11142,626,0006,089,0002,285,568
27Dec 30, 2025 04:453,84373,336,24245,958,0003,996,0002,342,912