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 listMar 21, 2026 19:40Yuriy LyfenkoYuriy LyfenkoScore: 2,295Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 21, 2026 19:402,14350,099,83423,876,0003,979,0002,433,024
2Mar 21, 2026 19:402,16854,128,12222,150,0006,040,0002,469,888
3Mar 21, 2026 19:402,16951,111,61524,173,0004,028,0002,334,720
4Mar 21, 2026 19:402,22551,186,59425,938,0002,992,0002,347,008
5Mar 21, 2026 19:402,24052,286,56424,096,0005,020,0002,428,928
6Mar 21, 2026 19:402,24853,073,86326,199,0003,023,0002,314,240
7Mar 21, 2026 19:402,26653,601,58424,378,0005,078,0002,428,928
8Mar 21, 2026 19:402,26852,806,31127,455,0002,033,0002,510,848
9Mar 21, 2026 19:402,27857,513,06224,509,0005,106,0002,371,584
10Mar 21, 2026 19:402,29555,008,63822,877,0006,962,0002,404,352
11Mar 21, 2026 19:402,31262,039,31027,051,0003,005,0002,326,528
12Mar 21, 2026 19:402,32461,372,43727,186,0003,020,0002,367,488
13Mar 21, 2026 19:402,35255,475,95024,465,0006,116,0002,461,696
14Mar 21, 2026 19:402,38057,043,98925,952,0004,990,0002,473,984
15Mar 21, 2026 19:402,38156,410,17527,963,0002,996,0002,510,848
16Mar 21, 2026 19:402,45356,234,56328,894,0002,989,0002,293,760
17Mar 21, 2026 19:402,50257,569,37928,463,0004,066,0002,392,064
18Mar 21, 2026 19:402,56859,050,04130,352,0003,035,0002,490,368