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:22Josu San MartinJosu San MartinScore: 8,099Success
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:146,409108,380,64375,285,0008,030,0002,293,760
2Dec 26, 2025 15:227,063120,565,62982,838,0008,982,0002,338,816
3Dec 26, 2025 15:227,257126,740,71092,328,0002,007,0002,457,600
4Dec 26, 2025 15:227,284125,811,96590,658,0004,029,0002,428,928
5Dec 26, 2025 15:227,360125,018,10192,662,0003,021,0002,310,144
6Dec 26, 2025 15:227,373124,634,02792,853,0002,995,0002,514,944
7Dec 26, 2025 15:167,374124,141,13686,778,0009,081,0002,322,432
8Dec 26, 2025 15:147,377123,848,13492,910,0002,997,0002,392,064
9Dec 26, 2025 15:227,389126,418,91091,053,0005,002,0002,498,560
10Dec 26, 2025 15:227,525128,965,43793,829,0003,992,0002,322,432
11Dec 26, 2025 15:147,644126,502,83094,356,0005,018,0002,379,776
12Dec 26, 2025 15:167,684121,835,73192,904,0006,992,0002,465,792
13Dec 26, 2025 15:227,714128,061,12295,266,0005,014,0002,396,160
14Dec 26, 2025 15:227,766123,693,88697,961,0002,998,0002,433,024
15Dec 26, 2025 15:167,780124,533,15497,130,0004,005,0002,363,392
16Dec 26, 2025 15:167,791133,830,61997,269,0004,011,0002,330,624
17Dec 26, 2025 15:227,832127,538,39397,819,0003,992,0002,527,232
18Dec 26, 2025 15:227,834132,932,27793,858,0007,987,0002,420,736
19Dec 26, 2025 15:227,938129,149,64298,191,0005,009,0002,281,472
20Dec 26, 2025 15:148,016132,896,71799,203,0005,010,0002,420,736
21Dec 26, 2025 15:148,033137,119,483102,424,0002,008,0002,486,272
22Dec 26, 2025 15:228,037133,455,766100,459,0004,018,0002,457,600
23Dec 26, 2025 15:168,050130,146,941100,625,0004,025,0002,412,544
24Dec 26, 2025 15:148,090133,706,128100,158,0005,007,0002,330,624
25Dec 26, 2025 15:228,099133,272,174103,277,0002,005,0002,334,720
26Dec 26, 2025 15:228,120132,711,359103,548,0002,010,0002,408,448
27Dec 26, 2025 15:218,224130,064,934101,914,0004,995,0002,510,848
28Dec 26, 2025 15:148,246135,033,217105,193,0002,003,0002,510,848
29Dec 26, 2025 15:228,280134,311,162106,631,0001,005,0002,412,544
30Dec 26, 2025 15:168,293133,946,451103,815,0003,992,0002,514,944
31Dec 26, 2025 15:228,297141,409,462101,875,0005,992,0002,404,352
32Dec 26, 2025 15:168,415137,014,154108,389,0001,003,0002,351,104
33Dec 26, 2025 15:168,416136,286,951106,397,0003,011,0002,486,272
34Dec 26, 2025 15:228,451141,655,432104,873,0004,993,0002,375,680
35Dec 26, 2025 15:228,469139,482,826108,099,0002,001,0002,379,776
36Dec 26, 2025 15:168,483141,006,908106,269,0004,010,0002,273,280
37Dec 26, 2025 15:168,494143,988,729109,421,0001,003,0002,293,760
38Dec 26, 2025 15:228,525143,888,592107,828,0002,995,0002,449,408
39Dec 26, 2025 15:218,678137,692,552109,822,0002,995,0002,457,600
40Dec 26, 2025 15:218,727141,740,433108,437,0005,020,0002,457,600
41Dec 26, 2025 15:168,729140,984,330106,448,0007,029,0002,494,464
42Dec 26, 2025 15:228,760136,690,535108,889,0004,994,0002,482,176
43Dec 26, 2025 15:149,012142,920,528112,149,0005,006,0002,359,296
44Dec 26, 2025 15:229,051145,040,366109,613,0008,045,0002,449,408
45Dec 26, 2025 15:149,064149,238,784109,848,0007,988,0002,506,752
46Dec 26, 2025 15:169,095149,463,115116,237,0002,004,0002,445,312
47Dec 26, 2025 15:229,279153,135,390114,598,0006,031,0002,514,944
48Dec 26, 2025 15:229,292149,825,563116,801,0003,993,0002,523,136