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 29, 2025 01:12Josu San MartinJosu San MartinScore: 4,203Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 01:123,85382,008,84148,080,0002,003,0002,469,888
2Dec 29, 2025 01:133,88072,574,18748,422,0002,017,0002,269,184
3Dec 29, 2025 01:133,96577,195,01449,523,0002,021,0002,273,280
4Dec 29, 2025 01:113,96779,532,62448,543,0003,033,0002,416,640
5Dec 29, 2025 01:123,97172,365,75548,586,0003,036,0002,498,560
6Dec 29, 2025 01:113,99076,707,39347,878,0003,989,0002,510,848
7Dec 29, 2025 01:134,08782,024,81849,122,0004,009,0002,289,664
8Dec 29, 2025 01:134,14679,527,35047,911,0005,988,0002,355,200
9Dec 29, 2025 01:064,18875,899,93252,433,0002,016,0002,383,872
10Dec 29, 2025 01:064,20381,085,53853,628,0001,011,0002,301,952
11Dec 29, 2025 01:064,38083,689,30754,942,0001,997,0002,486,272
12Dec 29, 2025 01:124,40884,260,05753,279,0004,021,0002,449,408
13Dec 29, 2025 01:124,42885,020,93853,525,0004,039,0002,265,088
14Dec 29, 2025 01:134,49478,246,30755,405,0003,022,0002,351,104
15Dec 29, 2025 01:124,53188,288,17954,906,0003,993,0002,494,464
16Dec 29, 2025 01:114,64886,418,83259,420,0001,007,0002,441,216
17Dec 29, 2025 01:134,94891,476,67859,294,0005,024,0002,473,984
18Dec 29, 2025 01:125,396100,915,18268,145,0002,004,0002,449,408