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 30, 2025 00:31yenw0dyenw0dScore: 3,144Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 30, 2025 00:322,81359,442,43236,575,00002,314,240
2Dec 30, 2025 00:312,84462,649,84034,969,0001,998,0002,322,432
3Dec 30, 2025 00:322,99165,566,68935,893,0002,991,0002,482,176
4Dec 30, 2025 00:323,02062,960,57137,249,0002,013,0002,338,816
5Dec 30, 2025 00:313,05772,795,95939,747,00002,342,912
6Dec 30, 2025 00:313,06969,558,15438,899,000997,0002,482,176
7Dec 30, 2025 00:323,08266,257,70637,066,0003,005,0002,265,088
8Dec 30, 2025 00:323,11362,315,61835,413,0005,059,0002,342,912
9Dec 30, 2025 00:313,13068,749,21638,651,0002,034,0002,441,216
10Dec 30, 2025 00:323,14468,188,53939,872,000996,0002,478,080
11Dec 30, 2025 00:323,14968,062,95538,942,0001,997,0002,461,696
12Dec 30, 2025 00:313,17766,651,89038,278,0003,022,0002,506,752
13Dec 30, 2025 00:323,29778,435,35841,868,000996,0002,326,528
14Dec 30, 2025 00:313,29867,949,12640,886,0001,994,0002,478,080
15Dec 30, 2025 00:323,35265,549,98441,547,0002,026,0002,371,584
16Dec 30, 2025 00:313,37473,887,04641,868,0001,993,0002,428,928
17Dec 30, 2025 00:313,56869,150,25045,374,0001,008,0002,478,080
18Dec 30, 2025 00:313,62979,369,85244,162,0003,011,0002,273,280