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 29, 2025 15:25Josu San MartinJosu San MartinScore: 3,682Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 15:103,10666,824,48038,355,0002,018,0002,523,136
2Dec 29, 2025 15:183,28471,579,30738,628,0004,066,0002,441,216
3Dec 29, 2025 15:193,31069,162,11940,026,0003,001,0002,437,120
4Dec 29, 2025 15:143,31367,889,87140,069,0003,005,0002,334,720
5Dec 29, 2025 15:283,32070,489,63340,150,0003,011,0002,498,560
6Dec 29, 2025 15:043,34266,096,51341,429,0002,020,0002,375,680
7Dec 29, 2025 15:313,34666,416,74640,459,0003,034,0002,330,624
8Dec 29, 2025 15:223,37669,417,08142,869,0001,020,0002,379,776
9Dec 29, 2025 15:103,38068,799,76639,940,0003,994,0002,408,448
10Dec 29, 2025 15:083,38571,534,09141,003,0003,000,0002,449,408
11Dec 29, 2025 15:053,39675,088,41641,133,0003,009,0002,416,640
12Dec 29, 2025 15:143,42166,106,64341,439,0003,032,0002,478,080
13Dec 29, 2025 15:313,42666,086,30840,493,0004,049,0002,404,352
14Dec 29, 2025 15:243,43477,341,27742,608,0002,028,0002,449,408
15Dec 29, 2025 15:053,46174,349,44939,996,0004,999,0002,400,256
16Dec 29, 2025 15:283,47876,658,36242,198,0003,014,0002,363,392
17Dec 29, 2025 15:193,49366,203,17743,389,0002,018,0002,445,312
18Dec 29, 2025 15:213,50672,900,47141,528,0004,051,0002,404,352
19Dec 29, 2025 15:243,52877,860,76444,866,000997,0002,506,752
20Dec 29, 2025 15:273,52870,913,99041,878,0003,988,0002,297,856
21Dec 29, 2025 15:053,53777,115,21240,983,0004,997,0002,424,832
22Dec 29, 2025 15:133,53877,289,34943,996,0001,999,0002,510,848
23Dec 29, 2025 15:083,54777,118,14440,093,0006,014,0002,506,752
24Dec 29, 2025 15:133,54870,371,34644,118,0002,005,0002,400,256
25Dec 29, 2025 15:133,55174,774,79344,153,0002,006,0002,424,832
26Dec 29, 2025 15:043,55679,583,39044,215,0002,009,0002,322,432
27Dec 29, 2025 15:243,55778,525,20343,222,0003,015,0002,469,888
28Dec 29, 2025 15:193,57667,713,02943,453,0003,031,0002,310,144
29Dec 29, 2025 15:303,59472,593,63746,723,00002,465,792
30Dec 29, 2025 15:183,67079,990,07247,716,00002,281,472
31Dec 29, 2025 15:103,67279,739,93646,718,0001,015,0002,433,024
32Dec 29, 2025 15:043,68271,424,69242,883,0004,986,0002,510,848
33Dec 29, 2025 15:163,68373,292,68343,892,0003,990,0002,396,160
34Dec 29, 2025 15:253,69375,970,31744,011,0004,001,0002,342,912
35Dec 29, 2025 15:113,70273,616,18645,119,0003,007,0002,424,832
36Dec 29, 2025 15:223,72070,765,21842,318,0006,045,0002,469,888
37Dec 29, 2025 15:273,74182,061,63347,614,0001,013,0002,338,816
38Dec 29, 2025 15:113,74677,838,54147,683,0001,014,0002,416,640
39Dec 29, 2025 15:073,75173,223,87346,728,0002,031,0002,523,136
40Dec 29, 2025 15:303,75975,052,84747,875,000997,0002,486,272
41Dec 29, 2025 15:313,77076,379,29345,007,0004,000,0002,449,408
42Dec 29, 2025 15:073,77173,822,09747,022,0002,000,0002,400,256
43Dec 29, 2025 15:333,77274,020,79646,037,0003,002,0002,428,928
44Dec 29, 2025 15:163,79377,566,89742,264,0007,044,0002,297,856
45Dec 29, 2025 15:303,79678,533,78947,329,0002,014,0002,387,968
46Dec 29, 2025 15:223,81981,707,98846,603,0003,039,0002,375,680
47Dec 29, 2025 15:333,82579,462,68546,679,0003,044,0002,502,656
48Dec 29, 2025 15:113,85776,172,25546,134,0004,011,0002,392,064
49Dec 29, 2025 15:213,87073,032,00747,292,0003,018,0002,424,832
50Dec 29, 2025 15:353,89478,450,49446,567,0004,049,0002,420,736
51Dec 29, 2025 15:333,89878,647,14448,651,0002,027,0002,453,504
52Dec 29, 2025 15:143,91779,665,16447,922,0002,995,0002,359,296
53Dec 29, 2025 15:273,93583,271,73049,151,0002,006,0002,314,240
54Dec 29, 2025 15:183,96377,509,17048,489,0003,030,0002,269,184
55Dec 29, 2025 15:283,97677,645,10350,672,0001,013,0002,383,872
56Dec 29, 2025 15:074,01878,916,85249,222,0003,013,0002,371,584
57Dec 29, 2025 15:354,01987,255,08548,231,0004,019,0002,379,776
58Dec 29, 2025 15:084,02583,644,36050,315,0002,012,0002,416,640
59Dec 29, 2025 15:164,06679,063,09747,874,0004,986,0002,269,184
60Dec 29, 2025 15:254,11282,533,18449,423,0004,034,0002,355,200
61Dec 29, 2025 15:254,16282,801,60649,097,0005,009,0002,375,680
62Dec 29, 2025 15:214,17982,849,60949,293,0005,029,0002,437,120
63Dec 29, 2025 15:354,27079,800,12652,480,0003,027,0002,383,872