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 05:22Yuriy LyfenkoYuriy LyfenkoScore: 3,439Success
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 05:222,99662,256,09532,956,0005,992,0002,408,448
2Mar 20, 2026 05:223,10966,489,89135,360,0005,051,0002,367,488
3Mar 20, 2026 05:223,13370,878,66136,662,0004,073,0002,359,296
4Mar 20, 2026 05:113,16667,495,03138,147,0003,011,0002,453,504
5Mar 20, 2026 05:113,20171,289,87637,553,0004,059,0002,416,640
6Mar 20, 2026 05:113,26066,847,53738,349,0004,036,0002,441,216
7Mar 20, 2026 05:223,32169,330,27338,149,0005,019,0002,387,968
8Mar 20, 2026 05:223,33569,565,03240,335,0003,025,0002,379,776
9Mar 20, 2026 05:223,34867,145,81638,461,0005,060,0002,301,952
10Mar 20, 2026 05:223,38067,438,77938,942,0004,992,0002,531,328
11Mar 20, 2026 05:223,38169,074,04140,882,0003,066,0002,441,216
12Mar 20, 2026 05:113,40366,299,96238,208,0006,032,0002,297,856
13Mar 20, 2026 05:113,40772,094,94939,257,0005,033,0002,314,240
14Mar 20, 2026 05:223,43974,045,57040,640,0004,064,0002,342,912
15Mar 20, 2026 05:223,45572,065,61539,924,0004,990,0002,371,584
16Mar 20, 2026 05:223,46066,933,87142,986,0001,999,0002,318,336
17Mar 20, 2026 05:223,46169,204,36439,991,0004,998,0002,330,624
18Mar 20, 2026 05:223,46674,018,12441,049,0004,004,0002,351,104
19Mar 20, 2026 05:223,50083,088,12539,567,0005,935,0002,318,336
20Mar 20, 2026 05:223,52670,614,94640,853,0004,982,0002,498,560
21Mar 20, 2026 05:113,53375,608,55142,932,0002,995,0002,310,144
22Mar 20, 2026 05:223,56672,631,54941,324,0005,039,0002,437,120
23Mar 20, 2026 05:113,57072,066,01742,372,0004,035,0002,379,776
24Mar 20, 2026 05:223,81273,976,01246,520,0003,033,0002,453,504
25Mar 20, 2026 05:113,91676,548,29246,910,0003,992,0002,301,952
26Mar 20, 2026 05:113,93179,454,53445,095,0006,012,0002,379,776
27Mar 20, 2026 05:223,93380,172,66647,117,0004,009,0002,420,736