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 18, 2025 14:34Aniruddha DebAniruddha DebScore: 296,857Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 18, 2025 14:34243,2473,192,685,8753,139,225,00022,987,000483,328
2Oct 18, 2025 14:34254,0373,328,096,5903,274,503,00027,978,000458,752
3Oct 18, 2025 14:34271,6443,566,957,6953,501,387,00029,986,000282,624
4Oct 18, 2025 14:33272,7283,579,197,6763,519,470,00025,988,000475,136
5Oct 18, 2025 14:33274,3493,605,781,1133,537,559,00028,980,000339,968
6Oct 18, 2025 14:33287,2843,771,813,4103,703,715,00030,980,000499,712
7Oct 18, 2025 14:35296,8573,887,654,4823,828,159,00030,977,000303,104
8Oct 18, 2025 14:34301,9293,959,060,4273,895,092,00029,985,000491,520
9Oct 18, 2025 14:35310,5034,062,017,5094,009,555,00026,983,000503,808
10Oct 18, 2025 14:35326,7944,278,766,3344,221,337,00026,982,000323,584
11Oct 18, 2025 14:34328,1944,302,096,0654,236,536,00029,982,000442,368
12Oct 18, 2025 14:34329,1624,310,969,6824,252,130,00026,981,000483,328