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 listOct 18, 2025 10:36Aniruddha DebAniruddha DebScore: 212,481Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 18, 2025 15:19164,1602,162,090,5902,121,083,00012,994,0002,486,272
2Oct 18, 2025 15:19180,6432,377,904,4972,334,367,00013,990,0002,453,504
3Oct 18, 2025 15:19182,9162,404,846,5882,371,911,0005,997,0002,306,048
4Oct 18, 2025 15:19188,7842,479,806,2022,437,202,00016,987,0002,297,856
5Oct 18, 2025 10:36190,1102,493,552,6562,463,439,0007,994,0002,400,256
6Oct 18, 2025 15:19193,1182,541,971,7562,502,535,0007,995,0002,461,696
7Oct 18, 2025 10:36198,4522,607,241,8392,569,876,0009,995,0002,510,848
8Oct 18, 2025 15:19202,0672,655,575,9302,611,877,00014,993,0002,265,088
9Oct 18, 2025 10:36210,1872,767,149,5702,723,440,0008,994,0002,408,448
10Oct 18, 2025 15:19210,8792,766,744,0692,731,434,0009,994,0002,383,872
11Oct 18, 2025 15:18212,4812,790,790,4732,748,259,00013,991,0002,273,280
12Oct 18, 2025 10:36212,7262,788,695,5872,759,437,0005,996,0002,371,584
13Oct 18, 2025 15:18218,8412,872,834,6832,834,939,0009,996,0002,363,392
14Oct 18, 2025 10:36222,0032,914,661,1252,876,046,0009,993,0002,387,968
15Oct 18, 2025 10:36225,1032,951,909,3602,914,351,00011,993,0002,326,528
16Oct 18, 2025 15:19228,5092,996,939,1872,959,619,00010,994,0002,416,640
17Oct 18, 2025 10:36229,2143,011,782,8902,969,793,0009,995,0002,424,832
18Oct 18, 2025 15:19233,0113,056,668,0493,015,149,00013,991,0002,379,776
19Oct 18, 2025 10:36234,1873,073,908,1903,029,446,00014,987,0002,453,504
20Oct 18, 2025 15:18235,4383,092,269,4203,050,703,0009,992,0002,318,336
21Oct 18, 2025 10:36241,3763,166,507,3183,129,890,0007,994,0002,408,448