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 25, 2025 16:29Josu San MartinJosu San MartinScore: 13,587Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 16:2811,157171,099,000141,041,0004,001,0002,482,176
2Dec 25, 2025 16:3011,933179,709,845148,122,0007,005,0002,396,160
3Dec 25, 2025 16:2912,123189,144,774153,578,0004,015,0002,277,376
4Dec 25, 2025 16:2812,276189,284,632154,564,0005,018,0002,297,856
5Dec 25, 2025 16:2812,277193,225,690153,585,0006,022,0002,502,656
6Dec 25, 2025 16:3012,433191,143,038157,638,0003,990,0002,269,184
7Dec 25, 2025 16:2912,453188,411,640157,886,0003,997,0002,494,464
8Dec 25, 2025 16:3012,496189,332,603162,444,00002,355,200
9Dec 25, 2025 16:3012,738193,544,858164,594,0001,003,0002,277,376
10Dec 25, 2025 16:2912,782196,188,320164,170,0002,002,0002,510,848
11Dec 25, 2025 16:3012,913191,614,447164,876,0002,997,0002,490,368
12Dec 25, 2025 16:2813,081198,757,638166,047,0004,001,0002,371,584
13Dec 25, 2025 16:2913,409199,895,307173,321,0001,001,0002,355,200
14Dec 25, 2025 16:2913,444201,330,009171,777,0002,996,0002,326,528
15Dec 25, 2025 16:3013,482204,532,536172,258,0003,004,0002,523,136
16Dec 25, 2025 16:2913,587206,206,951175,625,0001,003,0002,457,600
17Dec 25, 2025 16:3013,612205,461,982173,954,0002,999,0002,514,944
18Dec 25, 2025 16:2814,102209,839,788182,321,0001,001,0002,293,760
19Dec 25, 2025 16:3014,521216,509,014184,774,0003,995,0002,297,856
20Dec 25, 2025 16:2914,646219,566,857184,380,0006,012,0002,281,472
21Dec 25, 2025 16:2914,799221,165,757190,383,0002,004,0002,482,176
22Dec 25, 2025 16:2814,801227,454,370191,409,0001,002,0002,306,048
23Dec 25, 2025 16:3014,963228,279,618191,517,0003,008,0002,486,272
24Dec 25, 2025 16:2915,136224,034,081190,774,0005,992,0002,293,760
25Dec 25, 2025 16:2915,256223,915,161195,323,0003,004,0002,301,952
26Dec 25, 2025 16:2915,374227,219,400192,869,0006,995,0002,301,952
27Dec 25, 2025 16:2815,615229,496,423201,001,0002,000,0002,306,048
28Dec 25, 2025 16:2816,006237,390,137206,079,0002,000,0002,453,504
29Dec 25, 2025 16:2916,318241,620,739207,127,0005,003,0002,510,848
30Dec 25, 2025 16:2816,802246,145,885214,419,0004,007,0002,306,048