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 9, 2025 15:34Yuriy LyfenkoYuriy LyfenkoScore: 6,244Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 9, 2025 15:345,04999,935,07660,587,0005,048,0002,387,968
2Oct 9, 2025 15:345,53497,577,13965,948,0005,995,0002,514,944
3Oct 9, 2025 15:345,73296,477,11369,482,0005,034,0002,433,024
4Oct 9, 2025 15:345,765102,005,25570,954,0003,997,0002,322,432
5Oct 9, 2025 15:345,805109,255,10071,444,0004,025,0002,514,944
6Oct 9, 2025 15:345,879100,607,53572,400,0004,022,0002,269,184
7Oct 9, 2025 15:345,901104,822,51171,666,0005,046,0002,478,080
8Oct 9, 2025 15:345,992104,651,79272,908,0004,993,0002,277,376
9Oct 9, 2025 15:346,195108,161,71974,493,0006,039,0002,424,832
10Oct 9, 2025 15:346,199110,222,54875,554,0005,036,0002,523,136
11Oct 9, 2025 15:346,244108,347,31277,162,0004,008,0002,387,968
12Oct 9, 2025 15:346,395107,445,70679,131,0004,006,0002,490,368
13Oct 9, 2025 15:346,516117,331,96778,653,0006,050,0002,449,408
14Oct 9, 2025 15:346,570111,447,63980,383,0005,023,0002,392,064
15Oct 9, 2025 15:346,604112,206,24082,856,0002,994,0002,523,136
16Oct 9, 2025 15:346,629116,851,47181,165,0005,010,0002,355,200
17Oct 9, 2025 15:346,685113,925,85581,910,0004,994,0002,506,752
18Oct 9, 2025 15:346,875124,714,91083,354,0006,025,0002,322,432
19Oct 9, 2025 15:347,456127,074,42091,932,0004,996,0002,461,696
20Oct 9, 2025 15:347,667126,090,11695,649,0004,027,0002,490,368
21Oct 9, 2025 15:347,769129,038,37497,992,0002,999,0002,338,816