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 20, 2026 18:59Josu San MartinJosu San MartinScore: 2,476Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 20, 2026 18:592,31551,734,01324,080,0006,020,0002,437,120
2Mar 20, 2026 18:502,33256,936,16927,286,0003,031,0002,465,792
3Mar 20, 2026 18:502,35655,746,47928,584,0002,041,0002,306,048
4Mar 20, 2026 19:012,37555,140,41026,896,0003,984,0002,367,488
5Mar 20, 2026 18:502,38055,931,10926,952,0003,992,0002,473,984
6Mar 20, 2026 20:302,38054,764,52325,945,0004,989,0002,269,184
7Mar 20, 2026 19:012,38853,586,75426,038,0005,007,0002,428,928
8Mar 20, 2026 18:502,41958,413,99829,416,0002,028,0002,383,872
9Mar 20, 2026 18:502,42254,102,91627,426,0004,063,0002,297,856
10Mar 20, 2026 18:502,47663,557,60430,179,0002,011,0002,424,832
11Mar 20, 2026 19:012,48263,823,33629,247,0003,025,0002,527,232
12Mar 20, 2026 20:302,48959,834,92928,318,0004,045,0002,510,848
13Mar 20, 2026 18:502,52952,903,17830,885,0001,992,0002,449,408
14Mar 20, 2026 18:502,53056,811,41528,900,0003,986,0002,457,600
15Mar 20, 2026 20:302,56068,654,02029,242,0004,033,0002,433,024
16Mar 20, 2026 18:592,56858,945,87229,338,0004,046,0002,457,600
17Mar 20, 2026 18:592,61853,447,37432,032,0002,002,0002,523,136
18Mar 20, 2026 18:502,62566,256,12330,105,0004,014,0002,486,272