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 27, 2025 16:02Josu San MartinJosu San MartinScore: 9,796Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 27, 2025 16:038,097130,529,76398,243,0007,017,0002,310,144
2Dec 27, 2025 16:028,495139,839,035105,421,0005,020,0002,457,600
3Dec 27, 2025 16:048,817142,600,575105,569,0009,048,0002,514,944
4Dec 27, 2025 16:029,094149,759,150112,208,0006,011,0002,461,696
5Dec 27, 2025 16:029,203147,803,387115,622,0004,021,0002,461,696
6Dec 27, 2025 16:049,634149,487,701123,244,0002,003,0002,478,080
7Dec 27, 2025 16:039,796157,466,273125,339,0002,005,0002,514,944
8Dec 27, 2025 16:0410,267163,367,872131,463,0002,007,0002,506,752
9Dec 27, 2025 16:0310,463164,194,322131,013,0005,000,0002,379,776
10Dec 27, 2025 16:0210,554161,697,947134,203,0003,004,0002,519,040
11Dec 27, 2025 16:0210,622165,067,657133,081,0005,003,0002,408,448
12Dec 27, 2025 16:0210,754165,748,648131,808,0007,988,0002,498,560