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:57Yuriy LyfenkoYuriy LyfenkoScore: 2,570Success
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:572,33253,456,04024,253,0006,063,0002,314,240
2Mar 20, 2026 05:572,38757,110,65824,024,0007,007,0002,498,560
3Mar 20, 2026 05:572,39559,148,22126,116,0005,022,0002,416,640
4Mar 20, 2026 05:572,39556,931,64529,125,0002,008,0002,465,792
5Mar 20, 2026 05:572,39656,546,17326,122,0005,023,0002,392,064
6Mar 20, 2026 05:572,41263,877,27627,305,0004,045,0002,293,760
7Mar 20, 2026 05:572,41555,123,21926,330,0005,063,0002,437,120
8Mar 20, 2026 05:572,42756,093,65525,443,0006,106,0002,416,640
9Mar 20, 2026 05:572,42856,878,09527,493,0004,073,0002,392,064
10Mar 20, 2026 05:572,43360,982,62628,569,0003,060,0002,293,760
11Mar 20, 2026 05:572,45256,835,19128,892,0002,988,0002,519,040
12Mar 20, 2026 05:572,46054,795,42625,985,0005,996,0002,330,624
13Mar 20, 2026 05:572,47054,750,03325,086,0007,024,0002,387,968
14Mar 20, 2026 05:572,47757,411,18128,171,0004,024,0002,400,256
15Mar 20, 2026 05:572,50656,330,86928,502,0004,071,0002,461,696
16Mar 20, 2026 05:572,53355,294,91226,941,0005,986,0002,539,520
17Mar 20, 2026 05:572,55161,784,03227,130,0006,029,0002,539,520
18Mar 20, 2026 05:572,56264,270,00829,266,0004,036,0002,289,664
19Mar 20, 2026 05:572,57057,760,75329,355,0004,049,0002,445,312
20Mar 20, 2026 05:572,57063,158,08728,345,0005,061,0002,428,928
21Mar 20, 2026 05:572,57659,211,20926,388,0007,104,0002,379,776
22Mar 20, 2026 05:572,58463,098,83527,486,0006,108,0002,453,504
23Mar 20, 2026 05:572,60362,162,89530,764,0003,076,0002,437,120
24Mar 20, 2026 05:572,60466,607,43925,887,0007,965,0002,330,624
25Mar 20, 2026 05:572,60658,789,65130,890,0002,989,0002,293,760
26Mar 20, 2026 05:572,62158,596,85129,066,0005,011,0002,281,472
27Mar 20, 2026 05:572,62759,479,39729,129,0005,022,0002,412,544
28Mar 20, 2026 05:572,66659,519,89330,585,0004,078,0002,367,488
29Mar 20, 2026 05:572,66963,801,25130,613,0004,081,0002,424,832
30Mar 20, 2026 05:572,67458,124,48029,798,0004,966,0002,478,080
31Mar 20, 2026 05:572,67859,504,34628,842,0005,967,0002,510,848
32Mar 20, 2026 05:572,68658,834,73129,929,0004,988,0002,383,872
33Mar 20, 2026 05:572,69864,359,81731,065,0004,008,0002,519,040
34Mar 20, 2026 05:572,73464,249,69132,498,0003,046,0002,506,752
35Mar 20, 2026 05:572,73763,757,48831,514,0004,066,0002,461,696
36Mar 20, 2026 05:572,86767,208,83535,251,0002,014,0002,441,216