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 27, 2025 18:57Josu San MartinJosu San MartinScore: 5,142Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 27, 2025 18:584,33289,172,06454,311,0002,011,0002,387,968
2Dec 27, 2025 18:574,48490,569,54255,272,0003,014,0002,412,544
3Dec 27, 2025 18:574,69487,724,87058,026,0003,001,0002,420,736
4Dec 27, 2025 18:574,72087,581,56159,346,0002,011,0002,404,352
5Dec 27, 2025 18:584,78089,152,43557,134,0005,011,0002,486,272
6Dec 27, 2025 18:574,79789,774,08958,341,0004,023,0002,465,792
7Dec 27, 2025 18:584,91490,136,98960,891,0002,994,0002,363,392
8Dec 27, 2025 18:574,93689,592,00260,161,0004,010,0002,490,368
9Dec 27, 2025 18:585,03686,125,42662,444,0003,021,0002,457,600
10Dec 27, 2025 18:575,05392,725,91161,644,0004,042,0002,273,280
11Dec 27, 2025 18:575,10693,909,84360,342,0006,034,0002,322,432
12Dec 27, 2025 18:585,11888,020,15662,502,0004,032,0002,322,432
13Dec 27, 2025 18:585,14294,937,29061,854,0004,988,0002,379,776
14Dec 27, 2025 18:585,23494,361,15365,042,0003,001,0002,449,408
15Dec 27, 2025 18:585,29793,252,84066,863,0001,995,0002,408,448
16Dec 27, 2025 18:575,392101,391,79667,094,0003,004,0002,326,528
17Dec 27, 2025 18:575,41694,335,08966,388,0004,023,0002,519,040
18Dec 27, 2025 18:575,44797,063,12167,815,0002,991,0002,482,176
19Dec 27, 2025 18:585,45198,586,50167,864,0002,994,0002,494,464
20Dec 27, 2025 18:585,57096,245,07170,400,0002,011,0002,498,560
21Dec 27, 2025 18:585,60498,691,64168,865,0003,992,0002,400,256
22Dec 27, 2025 18:575,800104,218,63272,384,0003,016,0002,347,008
23Dec 27, 2025 18:576,066110,001,17675,862,0002,994,0002,404,352
24Dec 27, 2025 18:586,848119,705,01786,020,0003,000,0002,359,296