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 6, 2025 11:37Yuriy LyfenkoYuriy LyfenkoScore: 66,674Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 6, 2025 11:4558,928790,448,182752,064,00014,001,0002,383,872
2Oct 6, 2025 11:4560,731818,144,576780,515,0008,994,0002,310,144
3Oct 6, 2025 11:1461,179824,379,209787,333,0007,993,0002,506,752
4Oct 6, 2025 11:4561,958833,062,171796,466,0008,993,0002,473,984
5Oct 6, 2025 11:4562,135830,276,933802,763,0004,998,0002,285,568
6Oct 6, 2025 11:3663,639856,154,647818,311,0008,992,0002,379,776
7Oct 6, 2025 11:4563,888859,191,465820,555,0009,994,0002,482,176
8Oct 6, 2025 11:1463,927857,564,268822,054,0009,000,0002,424,832
9Oct 6, 2025 11:4564,911869,222,900836,848,0006,998,0002,318,336
10Oct 6, 2025 11:4566,068885,778,116850,881,0007,998,0002,412,544
11Oct 6, 2025 11:3666,674895,198,668857,760,0008,997,0002,334,720
12Oct 6, 2025 11:3767,358899,568,671864,663,00010,995,0002,486,272
13Oct 6, 2025 11:3769,093923,898,210882,218,00015,985,0002,498,560
14Oct 6, 2025 11:3770,221936,803,045900,877,00011,998,0002,371,584
15Oct 6, 2025 11:4570,440942,981,638904,728,00010,996,0002,277,376
16Oct 6, 2025 11:3773,482986,221,588944,274,00010,991,0002,310,144
17Oct 6, 2025 11:3676,2491,020,129,945975,246,00015,987,0002,433,024
18Oct 6, 2025 11:3776,6121,018,510,413987,955,0007,999,0002,510,848
19Oct 6, 2025 11:4579,5691,061,542,3471,020,403,00013,991,0002,514,944
20Oct 6, 2025 11:1480,4261,070,171,2311,034,543,00010,995,0002,400,256
21Oct 6, 2025 11:3785,1431,137,664,3921,096,860,0009,998,0002,342,912