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 listOct 13, 2025 16:24imotw2imotw2Score: 15,731Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 13, 2025 00:3712,700188,391,602160,096,0005,003,0002,494,464
2Oct 13, 2025 00:3712,746191,024,326158,705,0006,987,0002,498,560
3Oct 13, 2025 00:3613,084193,336,573166,093,0004,002,0002,490,368
4Oct 13, 2025 15:5213,835205,834,773175,858,0003,996,0002,494,464
5Oct 13, 2025 00:3413,940209,112,151177,215,0004,004,0002,457,600
6Oct 13, 2025 00:3714,072212,355,677177,932,0004,998,0002,306,048
7Oct 13, 2025 16:2414,165216,210,200181,145,0003,002,0002,437,120
8Oct 13, 2025 00:3714,200208,570,820179,590,0005,016,0002,433,024
9Oct 13, 2025 00:3414,409212,179,832185,317,0002,003,0002,420,736
10Oct 13, 2025 00:3414,569216,726,425185,392,0004,008,0002,297,856
11Oct 13, 2025 00:3314,574214,409,946185,451,0004,009,0002,318,336
12Oct 13, 2025 00:3714,627214,631,958184,146,0006,004,0002,387,968
13Oct 13, 2025 15:5214,646213,672,822185,390,0005,010,0002,453,504
14Oct 13, 2025 16:2414,746216,189,654186,706,0004,992,0002,416,640
15Oct 13, 2025 00:3715,059225,268,207192,774,0002,996,0002,494,464
16Oct 13, 2025 00:3315,094223,894,671190,211,0006,006,0002,506,752
17Oct 13, 2025 00:3615,110221,923,022189,415,0007,015,0002,310,144
18Oct 13, 2025 00:3415,170224,036,271193,208,0004,004,0002,457,600
19Oct 13, 2025 15:5215,185221,831,520192,399,0005,010,0002,461,696
20Oct 13, 2025 15:5115,189224,707,791197,451,00002,314,240
21Oct 13, 2025 00:3315,362224,726,902192,722,0006,989,0002,449,408
22Oct 13, 2025 16:2415,384229,609,461194,996,0004,999,0002,482,176
23Oct 13, 2025 00:3315,557226,536,103198,235,0004,004,0002,408,448
24Oct 13, 2025 15:5215,712231,085,676198,255,0006,007,0002,482,176
25Oct 13, 2025 00:3415,731231,756,868200,489,0004,009,0002,420,736
26Oct 13, 2025 15:5115,831231,469,673200,812,0004,995,0002,379,776
27Oct 13, 2025 00:3415,833230,536,490200,839,0004,996,0002,457,600
28Oct 13, 2025 00:3715,840232,648,449201,920,0003,998,0002,494,464
29Oct 13, 2025 16:2415,964238,482,591199,517,0008,020,0002,379,776
30Oct 13, 2025 00:3416,151237,526,474204,968,0004,999,0002,297,856
31Oct 13, 2025 15:5116,167238,198,354207,168,0003,002,0002,433,024
32Oct 13, 2025 00:3616,253238,810,504206,277,0005,006,0002,433,024
33Oct 13, 2025 15:5216,395239,634,793206,128,0007,004,0002,363,392
34Oct 13, 2025 00:3316,462240,397,270208,009,0006,000,0002,519,040
35Oct 13, 2025 16:2416,486239,904,408211,320,0003,004,0002,473,984
36Oct 13, 2025 00:3316,530241,509,132205,899,0008,995,0002,383,872
37Oct 13, 2025 00:3316,617247,286,628212,021,0004,000,0002,469,888
38Oct 13, 2025 16:2416,749248,080,677213,740,0003,995,0002,289,664
39Oct 13, 2025 16:2417,028247,566,149218,361,0003,004,0002,416,640
40Oct 13, 2025 00:3417,043247,035,743216,571,0004,990,0002,310,144
41Oct 13, 2025 16:2417,061246,014,109215,804,0005,994,0002,400,256
42Oct 13, 2025 00:3717,177253,286,383219,297,0004,005,0002,347,008
43Oct 13, 2025 16:2417,191248,724,936219,480,0004,008,0002,445,312
44Oct 13, 2025 00:3417,201250,803,595218,627,0004,991,0002,433,024
45Oct 13, 2025 15:5217,702259,394,474224,125,0006,003,0002,396,160
46Oct 13, 2025 00:3317,790257,849,933227,261,0004,004,0002,457,600
47Oct 13, 2025 00:3318,139264,632,426232,813,0002,997,0002,379,776
48Oct 13, 2025 00:3718,972272,236,363241,639,0004,992,0002,387,968