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 30, 2025 19:11Josu San MartinJosu San MartinScore: 3,551Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 30, 2025 19:013,06665,806,08836,871,0002,989,0002,318,336
2Dec 30, 2025 19:013,11061,669,21937,398,0003,032,0002,510,848
3Dec 30, 2025 19:113,11463,352,43239,469,0001,012,0002,400,256
4Dec 30, 2025 19:223,18566,072,05636,354,0005,049,0002,482,176
5Dec 30, 2025 19:123,18670,973,89637,377,0004,040,0002,465,792
6Dec 30, 2025 19:133,23867,396,66241,089,0001,002,0002,355,200
7Dec 30, 2025 19:113,37373,915,20641,851,0001,992,0002,387,968
8Dec 30, 2025 19:283,37568,796,62541,880,0001,994,0002,412,544
9Dec 30, 2025 19:163,38868,628,49142,042,0002,002,0002,387,968
10Dec 30, 2025 19:133,43769,936,27940,619,0004,061,0002,355,200
11Dec 30, 2025 19:123,45971,633,35341,968,0002,997,0002,408,448
12Dec 30, 2025 19:123,47174,371,00641,117,0004,011,0002,473,984
13Dec 30, 2025 19:113,49073,115,13644,358,0001,008,0002,301,952
14Dec 30, 2025 19:383,55170,574,78742,152,0004,014,0002,301,952
15Dec 30, 2025 19:113,57573,147,00141,429,0005,052,0002,482,176
16Dec 30, 2025 19:283,58172,157,75444,528,0002,024,0002,367,488
17Dec 30, 2025 19:013,59173,073,37944,655,0002,029,0002,437,120
18Dec 30, 2025 19:133,60674,480,01143,886,0002,992,0002,396,160
19Dec 30, 2025 19:223,68375,906,49945,883,0001,994,0002,342,912
20Dec 30, 2025 19:163,70173,140,27747,113,0001,002,0002,494,464
21Dec 30, 2025 19:383,72379,769,68943,362,0005,042,0002,404,352
22Dec 30, 2025 19:223,76773,595,52845,969,0002,998,0002,437,120
23Dec 30, 2025 19:383,77877,840,10945,105,0004,009,0002,310,144
24Dec 30, 2025 19:283,83478,480,54746,858,0002,990,0002,379,776
25Dec 30, 2025 19:113,84978,194,61446,036,0004,003,0002,473,984
26Dec 30, 2025 19:163,95178,639,13148,336,0003,021,0002,359,296
27Dec 30, 2025 19:113,96473,801,01948,498,0003,031,0002,342,912