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 14:27Josu San MartinJosu San MartinScore: 3,759Success
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 14:303,19168,010,35939,458,0002,023,0002,523,136
2Dec 29, 2025 14:303,24372,220,85140,151,0002,007,0002,355,200
3Dec 29, 2025 14:303,28368,926,13640,641,0002,032,0002,502,656
4Dec 29, 2025 14:283,39372,247,13543,106,0001,002,0002,301,952
5Dec 29, 2025 14:263,45169,156,28342,867,0001,993,0002,490,368
6Dec 29, 2025 14:263,48372,402,20440,245,0005,030,0002,363,392
7Dec 29, 2025 14:303,49073,979,64040,325,0005,040,0002,433,024
8Dec 29, 2025 14:293,49267,641,81943,374,0002,017,0002,412,544
9Dec 29, 2025 14:253,52976,753,81739,892,0005,983,0002,293,760
10Dec 29, 2025 14:293,57073,940,53242,380,0004,036,0002,359,296
11Dec 29, 2025 14:303,60880,760,08542,913,0003,991,0002,269,184
12Dec 29, 2025 14:263,64872,907,22641,374,0006,054,0002,510,848
13Dec 29, 2025 14:293,68377,376,79944,893,0002,992,0002,465,792
14Dec 29, 2025 14:303,69076,015,88244,978,0002,998,0002,383,872
15Dec 29, 2025 14:303,71373,755,15445,257,0003,017,0002,306,048
16Dec 29, 2025 14:253,72072,758,02446,349,0002,015,0002,449,408
17Dec 29, 2025 14:283,75980,942,59247,872,000997,0002,330,624
18Dec 29, 2025 14:273,76574,052,61445,952,0002,996,0002,322,432
19Dec 29, 2025 14:303,77979,481,43347,124,0002,005,0002,359,296
20Dec 29, 2025 14:283,80571,936,33844,421,0005,047,0002,445,312
21Dec 29, 2025 14:273,81775,852,28946,579,0003,037,0002,396,160
22Dec 29, 2025 14:273,84180,294,23748,936,000998,0002,400,256
23Dec 29, 2025 14:263,88078,524,32947,413,0003,026,0002,306,048
24Dec 29, 2025 14:283,98780,614,15147,843,0003,986,0002,322,432
25Dec 29, 2025 14:264,00375,631,21548,032,0004,002,0002,445,312
26Dec 29, 2025 14:274,00882,100,76346,091,0006,011,0002,306,048
27Dec 29, 2025 14:264,08683,651,17351,111,0002,004,0002,273,280
28Dec 29, 2025 14:284,15283,323,79951,974,0001,999,0002,363,392
29Dec 29, 2025 14:254,16576,832,20652,136,0002,005,0002,351,104
30Dec 29, 2025 14:304,16681,277,19650,150,0004,012,0002,351,104
31Dec 29, 2025 14:274,18477,415,40152,373,0002,014,0002,285,568
32Dec 29, 2025 14:284,41879,578,41952,400,0005,038,0002,433,024
33Dec 29, 2025 14:274,51585,201,08755,662,0003,036,0002,306,048