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 04:25Yuriy LyfenkoYuriy LyfenkoScore: 5,081Success
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 04:153,91276,294,90544,879,0005,983,0002,461,696
2Mar 20, 2026 04:204,27584,632,61551,535,0004,042,0002,506,752
3Mar 20, 2026 04:254,27777,360,10247,518,0008,088,0002,490,368
4Mar 20, 2026 04:204,29385,009,66150,830,0004,983,0002,392,064
5Mar 20, 2026 04:194,32886,036,38550,238,0006,028,0002,400,256
6Mar 20, 2026 04:154,45288,117,80250,896,0006,985,0002,371,584
7Mar 20, 2026 04:154,51083,500,51554,586,0004,043,0002,494,464
8Mar 20, 2026 04:254,51483,859,00655,643,0003,035,0002,433,024
9Mar 20, 2026 04:204,63487,272,71056,229,0004,016,0002,506,752
10Mar 20, 2026 04:204,68184,538,76954,866,0005,985,0002,457,600
11Mar 20, 2026 04:204,72584,614,84257,395,0004,027,0002,277,376
12Mar 20, 2026 04:254,74184,865,53158,603,0003,031,0002,392,064
13Mar 20, 2026 04:254,75792,468,13556,855,0004,987,0002,478,080
14Mar 20, 2026 04:254,77287,959,43259,031,0003,001,0002,428,928
15Mar 20, 2026 04:194,81288,461,44956,506,0006,054,0002,498,560
16Mar 20, 2026 04:194,83586,882,59056,871,0005,986,0002,478,080
17Mar 20, 2026 04:194,88192,008,35260,436,0003,021,0002,465,792
18Mar 20, 2026 04:204,94095,166,08260,209,0004,013,0002,523,136
19Mar 20, 2026 04:154,94894,095,15560,308,0004,020,0002,482,176
20Mar 20, 2026 04:194,96795,848,62958,523,0006,054,0002,351,104
21Mar 20, 2026 04:155,00098,845,49458,003,0007,000,0002,457,600
22Mar 20, 2026 04:195,08190,520,12762,054,0004,003,0002,392,064
23Mar 20, 2026 04:155,10291,665,63961,298,0005,024,0002,355,200
24Mar 20, 2026 04:255,11493,882,53364,462,0002,014,0002,453,504
25Mar 20, 2026 04:255,13097,030,73660,630,0006,063,0002,449,408
26Mar 20, 2026 04:155,16090,116,14962,076,0005,006,0002,502,656
27Mar 20, 2026 04:255,23098,936,64362,992,0004,999,0002,510,848
28Mar 20, 2026 04:255,26893,539,08165,469,0003,021,0002,494,464
29Mar 20, 2026 04:205,28493,240,54765,709,0002,986,0002,441,216
30Mar 20, 2026 04:255,30397,984,98361,950,0006,994,0002,465,792
31Mar 20, 2026 04:255,35997,219,15465,628,0004,038,0002,330,624
32Mar 20, 2026 04:155,437100,409,15965,628,0005,048,0002,359,296
33Mar 20, 2026 04:195,52596,706,29566,842,0004,988,0002,498,560
34Mar 20, 2026 04:255,54697,874,73366,096,0006,008,0002,359,296
35Mar 20, 2026 04:255,57798,841,80968,477,0004,028,0002,478,080
36Mar 20, 2026 04:255,591102,446,42567,639,0005,047,0002,281,472
37Mar 20, 2026 04:195,618101,411,04868,037,0005,002,0002,428,928
38Mar 20, 2026 04:205,624104,682,20868,105,0005,007,0002,281,472
39Mar 20, 2026 04:255,67096,307,92469,673,0004,039,0002,465,792
40Mar 20, 2026 04:205,715101,340,42369,270,0005,019,0002,457,600
41Mar 20, 2026 04:155,731102,311,59170,472,0004,027,0002,514,944
42Mar 20, 2026 04:195,824101,514,08669,657,0006,057,0002,314,240