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 18:26Josu San MartinJosu San MartinScore: 13,982Success
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 18:2511,483178,455,542145,273,0004,007,0002,326,528
2Dec 25, 2025 18:2512,868192,113,020163,273,0004,006,0002,514,944
3Dec 25, 2025 18:2713,007197,508,021168,088,0001,000,0002,342,912
4Dec 25, 2025 18:2513,599206,038,081171,789,0004,993,0002,514,944
5Dec 25, 2025 18:2713,726208,351,360175,435,0003,007,0002,277,376
6Dec 25, 2025 18:2613,753211,128,012176,791,0001,997,0002,359,296
7Dec 25, 2025 18:2513,891208,522,215179,584,0001,003,0002,510,848
8Dec 25, 2025 18:2513,932213,899,447177,118,0004,002,0002,301,952
9Dec 25, 2025 18:2513,947212,176,278178,302,0003,005,0002,514,944
10Dec 25, 2025 18:2613,982205,022,126179,768,0001,997,0002,326,528
11Dec 25, 2025 18:2514,027209,739,121180,347,0002,003,0002,277,376
12Dec 25, 2025 18:2614,059209,364,883180,775,0001,997,0002,514,944
13Dec 25, 2025 18:2514,248215,421,100183,227,0002,002,0002,453,504
14Dec 25, 2025 18:2614,358220,575,761182,641,0004,014,0002,306,048
15Dec 25, 2025 18:2514,386211,945,979184,023,0003,000,0002,412,544
16Dec 25, 2025 18:2714,478218,561,148185,208,0003,003,0002,293,760
17Dec 25, 2025 18:2614,717221,290,408186,308,0005,008,0002,502,656
18Dec 25, 2025 18:2615,091223,341,733193,187,0003,002,0002,486,272