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 26, 2025 01:09Josu San MartinJosu San MartinScore: 13,240Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 00:5710,948173,937,742137,316,0005,011,0002,396,160
2Dec 26, 2025 01:0911,434185,090,137146,636,0002,008,0002,306,048
3Dec 26, 2025 01:0311,741181,310,499149,627,0003,012,0002,527,232
4Dec 26, 2025 01:0812,524192,406,479158,822,0003,995,0002,494,464
5Dec 26, 2025 01:0912,547192,768,376160,114,0003,002,0002,502,656
6Dec 26, 2025 01:0312,832198,539,744162,821,0003,995,0002,359,296
7Dec 26, 2025 01:0812,907196,047,017164,792,0002,996,0002,510,848
8Dec 26, 2025 00:5713,140200,365,285166,824,0003,995,0002,506,752
9Dec 26, 2025 01:0813,160194,567,649165,079,0006,002,0002,400,256
10Dec 26, 2025 00:5713,208199,513,623169,711,0001,996,0002,527,232
11Dec 26, 2025 00:5713,240199,312,279170,119,0002,001,0002,277,376
12Dec 26, 2025 00:5713,392202,069,801170,091,0004,002,0002,510,848
13Dec 26, 2025 01:0913,395202,383,789168,131,0006,004,0002,322,432
14Dec 26, 2025 01:0913,523204,684,975170,799,0004,994,0002,351,104
15Dec 26, 2025 01:0313,849208,176,166174,031,0006,001,0002,449,408
16Dec 26, 2025 01:0914,132213,150,185178,730,0004,992,0002,306,048
17Dec 26, 2025 00:5714,231211,312,842181,005,0004,000,0002,510,848
18Dec 26, 2025 01:0914,292215,725,334182,804,0002,996,0002,428,928
19Dec 26, 2025 00:5715,174231,744,543193,257,0004,005,0002,351,104
20Dec 26, 2025 00:5715,852231,854,868200,080,0006,002,0002,371,584
21Dec 26, 2025 00:5715,985234,717,620203,807,0003,996,0002,482,176