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 22:36Josu San MartinJosu San MartinScore: 6,764Success
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 22:295,846103,647,55570,997,0004,999,0002,379,776
2Dec 26, 2025 22:295,974107,719,72773,628,0004,034,0002,510,848
3Dec 26, 2025 22:366,032106,796,13476,406,0002,010,0002,445,312
4Dec 26, 2025 22:286,064108,585,33676,833,0001,995,0002,306,048
5Dec 26, 2025 22:286,353112,767,67679,573,0003,021,0002,424,832
6Dec 26, 2025 22:296,359112,274,55179,647,0003,024,0002,506,752
7Dec 26, 2025 22:296,387107,549,96776,023,0007,002,0002,424,832
8Dec 26, 2025 22:286,437113,678,55979,648,0004,032,0002,392,064
9Dec 26, 2025 22:366,453115,312,20877,895,0005,991,0002,338,816
10Dec 26, 2025 22:296,514113,692,49281,654,0003,024,0002,412,544
11Dec 26, 2025 22:366,578111,881,79780,490,0005,030,0002,514,944
12Dec 26, 2025 22:366,591110,920,02284,675,0001,008,0002,375,680
13Dec 26, 2025 22:276,606113,589,07077,888,0007,988,0002,420,736
14Dec 26, 2025 22:276,689112,788,68476,965,0009,995,0002,297,856
15Dec 26, 2025 22:296,756112,846,08281,835,0005,987,0002,408,448
16Dec 26, 2025 22:296,764111,583,23182,935,0004,996,0002,428,928
17Dec 26, 2025 22:366,832121,767,72784,822,0003,991,0002,318,336
18Dec 26, 2025 22:286,854126,705,73986,097,0003,003,0002,433,024
19Dec 26, 2025 22:286,886115,465,08987,508,0002,011,0002,449,408
20Dec 26, 2025 22:366,938112,615,56683,180,0007,015,0002,502,656
21Dec 26, 2025 22:297,020118,710,46187,243,0004,011,0002,383,872
22Dec 26, 2025 22:297,030118,085,08587,368,0004,016,0002,506,752
23Dec 26, 2025 22:287,108123,754,89189,396,0003,013,0002,433,024
24Dec 26, 2025 22:297,190124,025,46891,463,0002,010,0002,322,432
25Dec 26, 2025 22:297,203122,543,59388,599,0005,034,0002,318,336
26Dec 26, 2025 22:277,255121,608,26189,302,0005,016,0002,441,216
27Dec 26, 2025 22:367,402122,674,07490,216,0006,014,0002,433,024
28Dec 26, 2025 22:367,590125,518,09090,610,0008,054,0002,441,216
29Dec 26, 2025 22:297,928134,244,40190,052,00013,007,0002,461,696
30Dec 26, 2025 22:368,222141,703,676101,894,0004,994,0002,420,736