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 02:20Josu San MartinJosu San MartinScore: 3,554Success
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 02:072,99872,359,35234,981,0003,997,0002,473,984
2Dec 30, 2025 01:583,13071,074,99636,624,0004,069,0002,338,816
3Dec 30, 2025 02:123,17565,120,70038,261,0003,020,0002,457,600
4Dec 30, 2025 02:073,23866,175,45640,094,0002,004,0002,392,064
5Dec 30, 2025 02:173,27663,356,49841,579,0001,014,0002,465,792
6Dec 30, 2025 02:123,27670,331,91140,558,0002,027,0002,416,640
7Dec 30, 2025 02:143,31568,837,15141,096,0002,004,0002,449,408
8Dec 30, 2025 02:143,34867,866,42640,484,0003,036,0002,318,336
9Dec 30, 2025 02:013,35272,626,10638,507,0005,066,0002,478,080
10Dec 30, 2025 02:073,37570,729,69340,879,0002,991,0002,297,856
11Dec 30, 2025 02:013,39769,844,43638,136,0006,021,0002,494,464
12Dec 30, 2025 02:013,40068,046,91941,181,0003,013,0002,433,024
13Dec 30, 2025 02:073,41169,459,82141,315,0003,023,0002,400,256
14Dec 30, 2025 02:023,42167,957,73840,436,0004,043,0002,449,408
15Dec 30, 2025 02:323,42570,867,15143,519,0001,012,0002,437,120
16Dec 30, 2025 02:023,42770,590,45841,512,0003,037,0002,490,368
17Dec 30, 2025 02:323,47671,025,45441,169,0004,016,0002,506,752
18Dec 30, 2025 02:073,48067,001,20244,237,0001,005,0002,416,640
19Dec 30, 2025 02:073,48277,091,13441,243,0004,023,0002,494,464
20Dec 30, 2025 02:073,49565,988,35342,401,0003,028,0002,441,216
21Dec 30, 2025 02:073,51176,182,16542,597,0003,042,0002,461,696
22Dec 30, 2025 02:083,53174,344,78542,906,0002,993,0002,482,176
23Dec 30, 2025 02:173,55470,863,31044,194,0002,008,0002,363,392
24Dec 30, 2025 02:013,56272,668,60042,279,0004,026,0002,465,792
25Dec 30, 2025 01:583,57265,935,25343,403,0003,028,0002,428,928
26Dec 30, 2025 02:083,61572,903,04143,995,0002,999,0002,506,752
27Dec 30, 2025 02:013,62377,015,91542,084,0005,010,0002,342,912
28Dec 30, 2025 02:083,62475,081,28642,100,0005,011,0002,498,560
29Dec 30, 2025 02:173,62974,894,48245,171,0002,007,0002,412,544
30Dec 30, 2025 02:143,66575,780,31145,621,0002,027,0002,424,832
31Dec 30, 2025 02:173,68372,442,22143,889,0003,989,0002,457,600
32Dec 30, 2025 02:073,68475,152,25843,897,0003,990,0002,371,584
33Dec 30, 2025 02:203,69074,175,01042,969,0004,996,0002,502,656
34Dec 30, 2025 02:203,71176,883,10745,228,0003,015,0002,408,448
35Dec 30, 2025 02:203,71867,191,50944,305,0004,027,0002,359,296
36Dec 30, 2025 02:073,73874,783,48145,556,0003,037,0002,379,776
37Dec 30, 2025 02:073,76074,486,05346,840,0002,036,0002,347,008
38Dec 30, 2025 02:013,76675,054,54744,961,0003,996,0002,326,528
39Dec 30, 2025 01:583,79374,556,23947,301,0002,012,0002,338,816
40Dec 30, 2025 02:323,79680,098,69244,308,0005,035,0002,383,872
41Dec 30, 2025 02:024,02074,296,92047,237,0005,025,0002,367,488
42Dec 30, 2025 02:174,17779,543,34451,286,0003,016,0002,494,464
43Dec 30, 2025 02:174,18779,528,85050,400,0004,032,0002,469,888
44Dec 30, 2025 02:124,23789,201,33750,074,0005,007,0002,433,024
45Dec 30, 2025 02:074,26982,038,34553,482,0002,018,0002,330,624