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 13, 2025 00:11imotw2imotw2Score: 16,312Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 13, 2025 00:0115,033220,530,294189,417,0006,013,0002,486,272
2Oct 13, 2025 00:1015,360226,159,117194,691,0004,992,0002,490,368
3Oct 13, 2025 00:1015,405228,511,937197,257,0003,003,0002,310,144
4Oct 13, 2025 00:1015,425228,664,037194,511,0006,015,0002,510,848
5Oct 13, 2025 00:0115,571228,398,615197,418,0005,010,0002,506,752
6Oct 13, 2025 00:0115,619236,465,716199,049,0004,000,0002,502,656
7Oct 13, 2025 00:1015,727231,146,082200,437,0004,008,0002,449,408
8Oct 13, 2025 00:0116,020236,614,761206,260,0002,002,0002,392,064
9Oct 13, 2025 00:0116,142237,180,820203,849,0005,995,0002,523,136
10Oct 13, 2025 00:1116,142241,185,406205,852,0003,997,0002,387,968
11Oct 13, 2025 00:1016,312239,104,446207,058,0005,001,0002,473,984
12Oct 13, 2025 00:1016,429240,765,274208,563,0005,013,0002,408,448
13Oct 13, 2025 00:1016,441245,271,976206,741,0006,991,0002,416,640
14Oct 13, 2025 00:0116,526243,115,640209,842,0004,996,0002,387,968
15Oct 13, 2025 00:0116,828246,645,340214,765,0003,995,0002,404,352
16Oct 13, 2025 00:1117,014247,675,953217,178,0004,003,0002,412,544
17Oct 13, 2025 00:0117,015243,700,822217,196,0004,003,0002,289,664
18Oct 13, 2025 00:1017,387254,989,313222,036,0004,000,0002,457,600
19Oct 13, 2025 00:0117,565257,039,948226,345,0002,003,0002,416,640
20Oct 13, 2025 00:1017,801257,191,332227,406,0004,007,0002,428,928
21Oct 13, 2025 00:1118,203264,616,935231,651,0004,992,0002,367,488