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 listApr 26, 2026 20:40TudyMTudyMScore: 297,048Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 26, 2026 20:41240,4303,145,202,3513,112,598,00012,994,0002,510,848
2Apr 26, 2026 20:41257,1353,362,135,8443,334,763,0007,994,0002,416,640
3Apr 26, 2026 20:41289,6483,787,970,5073,751,444,00013,990,0002,367,488
4Apr 26, 2026 20:41292,4173,823,944,0213,796,435,0004,996,0002,445,312
5Apr 26, 2026 20:41297,0483,881,938,9463,849,637,00011,992,0002,322,432
6Apr 26, 2026 20:41303,6203,968,572,5633,934,070,00012,993,0002,519,040
7Apr 26, 2026 20:41304,6173,983,332,6003,949,031,00010,994,0002,490,368
8Apr 26, 2026 20:41315,9824,130,450,5894,096,772,00010,994,0002,527,232
9Apr 26, 2026 20:41333,4434,356,940,1474,315,775,00018,990,0002,404,352