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 29, 2025 00:36Josu San MartinJosu San MartinScore: 5,295Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 03:264,39383,774,43652,099,0005,009,0002,387,968
2Dec 29, 2025 00:354,79290,183,45354,254,0008,037,0002,347,008
3Dec 29, 2025 00:344,86493,199,72858,216,0005,018,0002,461,696
4Dec 29, 2025 00:334,88589,647,10759,476,0004,032,0002,519,040
5Dec 29, 2025 03:264,88992,175,33756,500,0007,062,0002,416,640
6Dec 29, 2025 00:364,99790,285,58460,969,0003,998,0002,428,928
7Dec 29, 2025 00:375,02394,367,03461,275,0004,018,0002,342,912
8Dec 29, 2025 00:375,10393,559,80163,324,0003,015,0002,404,352
9Dec 29, 2025 00:355,10999,121,45662,391,0004,025,0002,514,944
10Dec 29, 2025 00:335,19094,086,07663,438,0004,027,0002,412,544
11Dec 29, 2025 03:265,22097,225,52864,869,0002,993,0002,310,144
12Dec 29, 2025 00:375,243100,024,44265,147,0003,006,0002,273,280
13Dec 29, 2025 00:335,24887,347,34367,222,0001,003,0002,461,696
14Dec 29, 2025 00:335,29596,155,60065,838,0002,992,0002,392,064
15Dec 29, 2025 00:335,297102,104,45267,858,000997,0002,502,656
16Dec 29, 2025 00:355,30693,430,17061,982,0006,998,0002,469,888
17Dec 29, 2025 00:345,310101,138,37763,026,0006,002,0002,392,064
18Dec 29, 2025 00:355,31490,315,45466,084,0003,003,0002,277,376
19Dec 29, 2025 00:365,33497,075,51963,309,0006,029,0002,347,008
20Dec 29, 2025 00:355,38199,330,85366,960,0002,998,0002,449,408
21Dec 29, 2025 00:365,548100,749,36965,118,0007,012,0002,310,144
22Dec 29, 2025 00:365,57498,772,51670,444,0002,012,0002,510,848
23Dec 29, 2025 00:365,79197,858,17570,265,0005,018,0002,486,272
24Dec 29, 2025 00:336,011102,237,70774,133,0004,007,0002,473,984
25Dec 29, 2025 00:356,02998,362,34974,358,0004,019,0002,498,560
26Dec 29, 2025 00:366,329108,369,67880,277,0002,006,0002,367,488
27Dec 29, 2025 00:346,583118,592,02780,544,0005,034,0002,519,040