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 17:32Josu San MartinJosu San MartinScore: 6,294Success
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 17:205,29791,550,02364,869,0003,991,0002,297,856
2Dec 26, 2025 17:205,481101,030,06067,243,0004,014,0002,383,872
3Dec 26, 2025 17:205,533102,423,66365,930,0005,993,0002,449,408
4Dec 26, 2025 17:175,627104,116,99370,142,0003,006,0002,310,144
5Dec 26, 2025 17:205,686105,329,20967,924,0005,993,0002,514,944
6Dec 26, 2025 17:175,770102,909,44867,005,0008,000,0002,297,856
7Dec 26, 2025 17:175,80998,538,94072,503,0003,020,0002,445,312
8Dec 26, 2025 17:325,867100,838,66570,251,0006,021,0002,482,176
9Dec 26, 2025 17:175,883111,636,58474,465,0002,012,0002,322,432
10Dec 26, 2025 17:175,911100,541,05272,852,0003,991,0002,387,968
11Dec 26, 2025 17:175,921101,154,15874,980,0001,999,0002,478,080
12Dec 26, 2025 17:175,931102,085,73271,101,0006,008,0002,314,240
13Dec 26, 2025 17:205,942112,389,03872,236,0005,016,0002,351,104
14Dec 26, 2025 17:176,108101,708,77376,394,0003,015,0002,465,792
15Dec 26, 2025 17:206,221106,471,64675,878,0004,992,0002,289,664
16Dec 26, 2025 17:176,245109,116,41574,166,0007,015,0002,494,464
17Dec 26, 2025 17:176,294109,518,20074,835,0006,984,0002,412,544
18Dec 26, 2025 17:206,295113,863,83077,843,0003,991,0002,490,368
19Dec 26, 2025 17:176,348111,723,89776,487,0006,038,0002,363,392
20Dec 26, 2025 17:176,355115,110,61581,607,0001,007,0002,334,720
21Dec 26, 2025 17:176,357116,396,52281,640,0001,007,0002,396,160
22Dec 26, 2025 17:176,494113,715,01781,405,0003,015,0002,453,504
23Dec 26, 2025 17:206,516118,991,78079,669,0005,042,0002,392,064
24Dec 26, 2025 17:206,660112,643,67280,543,0006,040,0002,277,376
25Dec 26, 2025 17:206,701119,091,37382,106,0005,006,0002,281,472
26Dec 26, 2025 17:206,888115,236,22386,531,0003,018,0002,314,240
27Dec 26, 2025 17:176,917114,921,42586,929,0002,997,0002,482,176
28Dec 26, 2025 17:326,918112,520,86184,938,0004,996,0002,310,144
29Dec 26, 2025 17:176,959116,490,42989,467,0001,005,0002,445,312
30Dec 26, 2025 17:177,003116,595,38387,035,0004,001,0002,281,472
31Dec 26, 2025 17:177,077118,511,28986,003,0006,000,0002,473,984
32Dec 26, 2025 17:327,174124,978,63888,243,0005,013,0002,457,600
33Dec 26, 2025 17:207,276123,050,89492,571,0002,012,0002,412,544