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 25, 2025 22:02Josu San MartinJosu San MartinScore: 13,793Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 22:0310,814173,107,228137,572,0003,012,0002,510,848
2Dec 25, 2025 22:0311,525179,515,754148,815,0001,005,0002,437,120
3Dec 25, 2025 22:0311,676179,675,457147,798,0003,994,0002,478,080
4Dec 25, 2025 22:0211,772176,648,694149,034,0004,000,0002,306,048
5Dec 25, 2025 22:0111,849183,951,814150,037,0004,000,0002,281,472
6Dec 25, 2025 22:0411,944188,460,372149,258,0006,010,0002,334,720
7Dec 25, 2025 22:0112,164190,590,547155,124,0003,002,0002,494,464
8Dec 25, 2025 22:0312,334188,485,951157,336,0003,006,0002,342,912
9Dec 25, 2025 22:0212,343187,462,130157,449,0003,008,0002,301,952
10Dec 25, 2025 22:0212,445187,620,190155,798,0005,992,0002,494,464
11Dec 25, 2025 22:0312,504192,121,504158,536,0004,013,0002,273,280
12Dec 25, 2025 22:0212,522194,080,283158,792,0003,994,0002,502,656
13Dec 25, 2025 22:0112,544183,558,719156,071,0007,003,0002,347,008
14Dec 25, 2025 22:0112,632189,917,281161,215,0003,004,0002,301,952
15Dec 25, 2025 22:0312,892195,833,316164,580,0003,010,0002,293,760
16Dec 25, 2025 22:0312,955195,389,358162,400,0006,014,0002,310,144
17Dec 25, 2025 22:0112,999196,371,656166,983,0001,999,0002,269,184
18Dec 25, 2025 22:0113,031197,536,691165,400,0004,009,0002,367,488
19Dec 25, 2025 22:0213,250195,032,191170,247,0002,002,0002,301,952
20Dec 25, 2025 22:0313,278205,781,092169,608,0003,010,0002,351,104
21Dec 25, 2025 22:0213,279198,847,978167,603,0005,018,0002,478,080
22Dec 25, 2025 22:0413,586202,466,290172,599,0004,013,0002,375,680
23Dec 25, 2025 22:0213,612202,425,318170,964,0005,998,0002,490,368
24Dec 25, 2025 22:0313,749204,124,995175,742,0002,995,0002,338,816
25Dec 25, 2025 22:0213,793212,873,467176,302,0003,005,0002,338,816
26Dec 25, 2025 22:0313,807212,217,840176,480,0003,008,0002,490,368
27Dec 25, 2025 22:0113,923207,324,122179,002,0002,000,0002,510,848
28Dec 25, 2025 22:0313,958215,113,669175,434,0006,014,0002,510,848
29Dec 25, 2025 22:0214,021209,409,116180,268,0002,002,0002,510,848
30Dec 25, 2025 22:0314,102205,070,296179,318,0004,007,0002,301,952
31Dec 25, 2025 22:0114,104209,042,826181,343,0002,003,0002,359,296
32Dec 25, 2025 22:0314,111214,806,797181,445,0002,004,0002,396,160
33Dec 25, 2025 22:0314,134222,643,111177,751,0005,991,0002,424,832
34Dec 25, 2025 22:0314,170209,857,393177,197,0007,007,0002,293,760
35Dec 25, 2025 22:0414,321214,121,733182,166,0004,003,0002,387,968
36Dec 25, 2025 22:0314,364210,176,745183,734,0002,995,0002,330,624
37Dec 25, 2025 22:0314,627220,363,852187,155,0003,002,0002,514,944
38Dec 25, 2025 22:0214,694214,885,616187,020,0004,000,0002,351,104
39Dec 25, 2025 22:0214,750220,393,602183,762,0007,989,0002,363,392
40Dec 25, 2025 22:0315,030223,980,298193,381,0002,003,0002,301,952
41Dec 25, 2025 22:0315,042223,963,351193,535,0002,005,0002,523,136
42Dec 25, 2025 22:0315,369230,418,942194,799,0004,994,0002,289,664
43Dec 25, 2025 22:0315,523233,569,658199,795,0002,007,0002,461,696
44Dec 25, 2025 22:0215,540229,666,919198,015,0004,000,0002,273,280
45Dec 25, 2025 22:0215,808233,044,799203,504,0002,004,0002,473,984
46Dec 25, 2025 22:0216,097239,172,675206,254,0003,003,0002,347,008
47Dec 25, 2025 22:0116,948251,667,879215,323,0005,007,0002,498,560
48Dec 25, 2025 22:0217,724263,827,253221,400,0009,016,0002,490,368