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 24, 2025 22:22Josu San MartinJosu San MartinScore: 14,582Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 24, 2025 22:1612,446187,559,542149,811,00011,984,0002,478,080
2Dec 24, 2025 22:1612,657191,727,554160,524,0004,013,0002,498,560
3Dec 24, 2025 22:2212,936193,199,612162,167,0006,006,0002,330,624
4Dec 24, 2025 22:2312,981195,562,366163,758,0004,992,0002,490,368
5Dec 24, 2025 22:2212,987196,073,296162,842,0005,994,0002,379,776
6Dec 24, 2025 22:1613,061202,106,821164,798,0004,993,0002,351,104
7Dec 24, 2025 22:2213,200199,352,294168,589,0003,010,0002,400,256
8Dec 24, 2025 22:1613,292206,449,293168,795,0003,995,0002,535,424
9Dec 24, 2025 22:2213,427206,099,662168,530,0006,018,0002,338,816
10Dec 24, 2025 22:2213,525204,245,941167,835,0007,992,0002,347,008
11Dec 24, 2025 22:1613,736205,345,459171,551,0007,022,0002,342,912
12Dec 24, 2025 22:1613,856210,958,728173,123,0007,004,0002,457,600
13Dec 24, 2025 22:1613,865202,988,382172,236,0008,011,0002,469,888
14Dec 24, 2025 22:2213,914210,331,071171,892,0008,994,0002,416,640
15Dec 24, 2025 22:2313,980208,915,468175,750,0005,991,0002,363,392
16Dec 24, 2025 22:1614,213218,356,246178,783,0005,992,0002,514,944
17Dec 24, 2025 22:2214,338213,516,591176,371,00010,021,0002,461,696
18Dec 24, 2025 22:1614,554218,513,773181,197,0008,008,0002,535,424
19Dec 24, 2025 22:2214,582216,861,637180,537,0009,026,0002,482,176
20Dec 24, 2025 22:1614,624218,699,717182,113,0008,005,0002,510,848
21Dec 24, 2025 22:1614,714222,233,445186,270,0005,007,0002,453,504
22Dec 24, 2025 22:2214,779221,210,926184,119,0008,005,0002,277,376
23Dec 24, 2025 22:2215,134224,781,185193,742,0002,996,0002,363,392
24Dec 24, 2025 22:2315,160225,450,753190,080,0007,002,0002,351,104
25Dec 24, 2025 22:1615,286226,921,469188,731,0009,985,0002,297,856
26Dec 24, 2025 22:1616,058238,422,319202,766,0005,993,0002,301,952
27Dec 24, 2025 22:1616,157236,544,373205,043,0005,001,0002,318,336
28Dec 24, 2025 22:2216,290243,354,107207,781,0003,995,0002,412,544
29Dec 24, 2025 22:1616,613244,038,506208,976,0006,999,0002,359,296
30Dec 24, 2025 22:1616,912248,968,524214,866,0004,996,0002,297,856
31Dec 24, 2025 22:2217,020253,829,758211,243,00010,011,0002,535,424
32Dec 24, 2025 22:2217,072263,279,445217,932,0003,998,0002,486,272
33Dec 24, 2025 22:2217,164252,875,087217,133,0006,003,0002,457,600
34Dec 24, 2025 22:1617,287254,091,062222,728,0001,997,0002,367,488
35Dec 24, 2025 22:1617,310247,721,459220,029,0005,000,0002,478,080
36Dec 24, 2025 22:2217,467265,063,349221,068,0006,001,0002,301,952