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:30Josu San MartinJosu San MartinScore: 8,218Success
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:286,365110,581,56277,758,0004,984,0002,412,544
2Dec 26, 2025 15:317,217119,821,51788,829,0004,990,0002,371,584
3Dec 26, 2025 15:287,239120,288,41191,106,0003,003,0002,297,856
4Dec 26, 2025 15:287,262125,306,69090,391,0004,017,0002,383,872
5Dec 26, 2025 15:287,355121,646,24891,585,0004,025,0002,482,176
6Dec 26, 2025 15:307,371123,034,99592,830,0002,994,0002,355,200
7Dec 26, 2025 15:287,422127,209,59892,471,0004,020,0002,469,888
8Dec 26, 2025 15:287,541122,554,07994,031,0004,001,0002,359,296
9Dec 26, 2025 15:287,544122,542,07894,065,0004,002,0002,465,792
10Dec 26, 2025 15:317,549126,545,69194,131,0004,005,0002,351,104
11Dec 26, 2025 15:317,643131,695,32893,336,0006,021,0002,502,656
12Dec 26, 2025 15:307,812133,118,22299,551,0002,011,0002,461,696
13Dec 26, 2025 15:287,837128,646,01197,885,0003,995,0002,351,104
14Dec 26, 2025 15:287,893133,913,41394,565,0008,048,0002,420,736
15Dec 26, 2025 15:307,907143,146,68498,795,0003,991,0002,334,720
16Dec 26, 2025 15:307,916129,155,33796,908,0005,994,0002,514,944
17Dec 26, 2025 15:287,996130,834,64399,951,0003,998,0002,330,624
18Dec 26, 2025 15:288,016132,752,32299,199,0005,010,0002,486,272
19Dec 26, 2025 15:288,020131,897,077101,258,0003,007,0002,506,752
20Dec 26, 2025 15:308,037131,484,447102,466,0002,009,0002,490,368
21Dec 26, 2025 15:288,081129,004,541103,053,0002,001,0002,367,488
22Dec 26, 2025 15:288,202133,054,055100,596,0006,035,0002,416,640
23Dec 26, 2025 15:308,218134,244,713100,846,0005,990,0002,289,664
24Dec 26, 2025 15:288,424137,328,574108,510,0001,004,0002,379,776
25Dec 26, 2025 15:318,490141,872,548108,364,0002,006,0002,330,624
26Dec 26, 2025 15:318,507135,462,885109,583,0001,005,0002,310,144
27Dec 26, 2025 15:288,548143,020,964107,126,0004,004,0002,330,624
28Dec 26, 2025 15:308,601139,645,501106,818,0004,991,0002,363,392
29Dec 26, 2025 15:288,753146,066,687106,805,0006,987,0002,441,216
30Dec 26, 2025 15:288,788147,717,855108,237,0006,013,0002,424,832
31Dec 26, 2025 15:318,968148,972,460113,568,0003,015,0002,482,176
32Dec 26, 2025 15:318,984143,560,744113,801,0002,994,0002,387,968
33Dec 26, 2025 15:288,985144,075,364110,819,0005,990,0002,514,944
34Dec 26, 2025 15:289,006159,056,188113,073,0004,002,0002,383,872
35Dec 26, 2025 15:289,027149,281,221115,349,0002,006,0002,310,144
36Dec 26, 2025 15:309,051146,791,084113,645,0004,022,0002,363,392
37Dec 26, 2025 15:289,079138,670,864113,021,0005,000,0002,465,792
38Dec 26, 2025 15:309,082147,309,180115,070,0003,001,0002,342,912
39Dec 26, 2025 15:319,088144,944,566112,140,0006,007,0002,445,312
40Dec 26, 2025 15:289,169140,106,057111,182,0008,013,0002,437,120
41Dec 26, 2025 15:309,244142,985,693119,175,0001,001,0002,371,584
42Dec 26, 2025 15:319,307142,585,371114,998,0005,999,0002,449,408
43Dec 26, 2025 15:289,328150,175,644119,265,0002,004,0002,387,968
44Dec 26, 2025 15:309,483159,511,044118,263,0005,011,0002,490,368
45Dec 26, 2025 15:309,819157,248,993124,630,0003,015,0002,506,752