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 25, 2025 04:10Josu San MartinJosu San MartinScore: 14,652Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 04:1010,859172,469,834134,158,0007,008,0002,433,024
2Dec 25, 2025 04:1012,193185,389,700152,489,0006,019,0002,453,504
3Dec 25, 2025 04:1012,529186,007,345157,880,0004,996,0002,441,216
4Dec 25, 2025 04:1013,320203,229,518170,163,0003,002,0002,330,624
5Dec 25, 2025 04:1013,805208,520,774175,457,0004,010,0002,301,952
6Dec 25, 2025 04:1014,143211,560,491179,868,0003,997,0002,465,792
7Dec 25, 2025 04:1014,475214,531,918186,179,0002,001,0002,457,600
8Dec 25, 2025 04:1014,652211,958,870187,472,0003,007,0002,514,944
9Dec 25, 2025 04:1015,056221,087,442191,738,0003,994,0002,498,560
10Dec 25, 2025 04:1015,570225,791,540200,405,0002,004,0002,433,024
11Dec 25, 2025 04:1015,709232,750,637199,217,0005,005,0002,473,984
12Dec 25, 2025 04:1015,810237,367,353203,525,0002,005,0002,387,968
13Dec 25, 2025 04:1015,861241,761,575202,189,0004,003,0002,293,760
14Dec 25, 2025 04:1016,214236,704,347206,783,0003,995,0002,424,832
15Dec 25, 2025 04:1016,365247,279,077210,754,0001,997,0002,273,280