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 26, 2025 15:38Josu San MartinJosu San MartinScore: 7,707Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 15:525,941106,355,62772,222,0005,015,0002,502,656
2Dec 26, 2025 15:326,131110,428,34575,668,0004,035,0002,379,776
3Dec 26, 2025 15:356,314111,760,06375,076,0007,007,0002,355,200
4Dec 26, 2025 15:326,504118,033,98583,542,0001,006,0002,428,928
5Dec 26, 2025 15:526,526107,314,93377,857,0006,987,0002,469,888
6Dec 26, 2025 15:356,527116,072,31879,858,0004,991,0002,408,448
7Dec 26, 2025 16:516,759115,234,46885,875,0001,997,0002,367,488
8Dec 26, 2025 15:386,773112,634,49282,046,0006,003,0002,293,760
9Dec 26, 2025 15:326,792121,379,28582,274,0006,020,0002,359,296
10Dec 26, 2025 16:516,865110,613,11083,229,0006,016,0002,494,464
11Dec 26, 2025 16:496,876114,414,91286,374,0003,013,0002,514,944
12Dec 26, 2025 15:356,926115,684,28484,037,0006,002,0002,359,296
13Dec 26, 2025 15:336,991113,288,85086,894,0003,995,0002,351,104
14Dec 26, 2025 16:527,015125,769,63389,185,0002,004,0002,473,984
15Dec 26, 2025 16:517,029120,803,59188,367,0003,012,0002,310,144
16Dec 26, 2025 15:327,040117,209,36687,498,0004,022,0002,506,752
17Dec 26, 2025 16:517,063121,623,38086,834,0004,990,0002,318,336
18Dec 26, 2025 15:327,077126,924,50387,004,0005,000,0002,367,488
19Dec 26, 2025 16:497,147122,003,87488,912,0003,996,0002,289,664
20Dec 26, 2025 15:387,154117,019,98085,006,0008,000,0002,396,160
21Dec 26, 2025 15:357,160121,733,06887,080,0006,005,0002,453,504
22Dec 26, 2025 16:547,233119,774,62588,028,0006,001,0002,297,856
23Dec 26, 2025 16:517,296122,294,28785,867,0008,986,0002,318,336
24Dec 26, 2025 15:337,299126,145,26587,892,0006,991,0002,519,040
25Dec 26, 2025 15:527,373126,343,70692,856,0002,995,0002,408,448
26Dec 26, 2025 15:527,379125,408,55085,936,0009,992,0002,441,216
27Dec 26, 2025 15:387,395116,324,39692,132,0004,005,0002,412,544
28Dec 26, 2025 15:357,412121,784,77493,346,0003,011,0002,355,200
29Dec 26, 2025 15:527,445123,524,02489,795,0006,984,0002,408,448
30Dec 26, 2025 16:567,450130,454,86792,853,0003,993,0002,506,752
31Dec 26, 2025 15:527,462121,322,07293,012,0004,000,0002,453,504
32Dec 26, 2025 16:517,478126,479,83590,194,0007,015,0002,514,944
33Dec 26, 2025 15:357,504123,898,60493,532,0004,022,0002,502,656
34Dec 26, 2025 15:337,507130,164,99893,566,0004,024,0002,457,600
35Dec 26, 2025 15:357,511125,951,66995,631,0002,013,0002,514,944
36Dec 26, 2025 15:527,526125,338,32293,840,0003,993,0002,416,640
37Dec 26, 2025 16:527,527125,822,72892,855,0004,992,0002,367,488
38Dec 26, 2025 15:357,531124,462,99591,911,0005,994,0002,310,144
39Dec 26, 2025 15:357,579126,012,98394,503,0004,021,0002,527,232
40Dec 26, 2025 15:357,586128,139,33194,592,0004,025,0002,461,696
41Dec 26, 2025 16:517,590129,555,78393,636,0005,034,0002,523,136
42Dec 26, 2025 16:507,651126,287,62097,450,0002,009,0002,281,472
43Dec 26, 2025 15:337,707129,833,76293,181,0007,013,0002,502,656
44Dec 26, 2025 15:327,727129,861,10396,430,0004,017,0002,510,848
45Dec 26, 2025 16:567,752128,911,61195,788,0004,988,0002,482,176
46Dec 26, 2025 16:517,809128,410,52298,508,0003,015,0002,416,640
47Dec 26, 2025 16:527,809129,248,82996,496,0005,025,0002,453,504
48Dec 26, 2025 15:357,832126,680,77994,832,0006,987,0002,375,680
49Dec 26, 2025 16:517,862127,486,32198,201,0004,008,0002,297,856
50Dec 26, 2025 15:357,864129,272,963100,234,0002,004,0002,387,968
51Dec 26, 2025 16:527,890123,724,60499,552,0003,016,0002,379,776
52Dec 26, 2025 15:527,916133,016,84497,912,0004,995,0002,351,104
53Dec 26, 2025 15:387,927132,605,00395,047,0008,004,0002,428,928
54Dec 26, 2025 15:357,931129,735,082100,096,0003,002,0002,371,584
55Dec 26, 2025 16:507,940129,189,95197,212,0006,013,0002,330,624
56Dec 26, 2025 16:517,959129,712,112101,456,0002,009,0002,433,024
57Dec 26, 2025 16:517,989136,064,41397,862,0005,991,0002,449,408
58Dec 26, 2025 15:358,060130,362,27999,793,0004,989,0002,392,064
59Dec 26, 2025 15:328,062132,035,365103,804,000998,0002,461,696
60Dec 26, 2025 16:528,069138,012,09198,899,0005,993,0002,514,944
61Dec 26, 2025 15:528,084127,849,60398,082,0007,005,0002,420,736
62Dec 26, 2025 15:528,139131,549,42096,829,0008,984,0002,322,432
63Dec 26, 2025 15:338,148132,582,583102,926,0002,997,0002,490,368
64Dec 26, 2025 16:548,217136,740,929104,820,0001,996,0002,445,312
65Dec 26, 2025 15:388,248129,311,793100,204,0007,014,0002,269,184
66Dec 26, 2025 15:338,256133,238,567102,310,0005,015,0002,326,528
67Dec 26, 2025 16:518,269134,508,396103,484,0004,018,0002,531,328
68Dec 26, 2025 15:328,273132,629,895101,515,0006,030,0002,482,176
69Dec 26, 2025 15:528,297142,597,140102,869,0004,993,0002,379,776
70Dec 26, 2025 15:388,447140,679,265105,817,0003,993,0002,457,600
71Dec 26, 2025 15:388,449135,232,385104,848,0004,992,0002,514,944
72Dec 26, 2025 15:358,449138,429,627103,790,0006,046,0002,453,504
73Dec 26, 2025 16:548,466136,449,711104,051,0006,002,0002,441,216
74Dec 26, 2025 16:498,497137,069,644104,435,0006,025,0002,437,120
75Dec 26, 2025 15:528,527138,585,897105,814,0005,038,0002,404,352
76Dec 26, 2025 16:568,629142,896,193105,166,0007,011,0002,363,392
77Dec 26, 2025 15:358,782141,214,566108,157,0006,008,0002,306,048
78Dec 26, 2025 15:328,837139,400,972108,884,0005,993,0002,297,856
79Dec 26, 2025 15:358,837140,337,576108,890,0005,993,0002,400,256
80Dec 26, 2025 15:358,982141,469,795112,769,0003,991,0002,330,624
81Dec 26, 2025 16:509,350157,834,376117,535,0004,018,0002,416,640
82Dec 26, 2025 15:389,459150,338,395117,974,0004,998,0002,306,048
83Dec 26, 2025 16:529,561148,501,423121,283,0003,007,0002,359,296
84Dec 26, 2025 15:3810,494162,646,718129,402,0007,021,0002,465,792