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 listNov 8, 2025 15:11Aniruddha DebAniruddha DebScore: 49,492Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 8, 2025 15:1245,114609,198,598582,484,0003,996,000393,216
2Nov 8, 2025 15:1145,388616,504,796589,049,0001,000,000380,928
3Nov 8, 2025 15:1145,838619,523,883593,892,0001,999,000270,336
4Nov 8, 2025 15:1247,482642,395,817616,268,0001,000,000405,504
5Nov 8, 2025 15:1848,015651,676,899624,198,0000299,008
6Nov 8, 2025 15:1248,069649,074,418624,900,0000311,296
7Nov 8, 2025 15:1148,184654,260,153622,393,0003,996,000331,776
8Nov 8, 2025 15:1148,512659,811,266627,663,0002,998,000348,160
9Nov 8, 2025 15:1848,869656,982,667634,294,0001,000,000483,328
10Nov 8, 2025 15:1249,013662,677,280637,165,0000376,832
11Nov 8, 2025 15:1249,101666,514,486637,319,0001,000,000434,176
12Nov 8, 2025 15:1249,379666,712,645638,931,0002,999,000294,912
13Nov 8, 2025 15:1849,385667,877,207642,009,0000352,256
14Nov 8, 2025 15:1849,492667,998,307641,397,0001,998,000290,816
15Nov 8, 2025 15:1850,196684,894,618650,555,0001,998,000266,240
16Nov 8, 2025 15:1150,752683,503,271658,778,000999,000307,200
17Nov 8, 2025 15:1152,032709,705,919673,423,0002,997,000278,528
18Nov 8, 2025 15:1152,404706,700,231678,246,0003,001,000299,008
19Nov 8, 2025 15:1853,161712,753,208691,090,0000323,584
20Nov 8, 2025 15:1853,303719,226,385692,942,0000315,392
21Nov 8, 2025 15:1254,937738,558,476712,189,0001,997,000401,408
22Nov 8, 2025 15:1855,633747,618,014721,230,0002,000,000258,048
23Nov 8, 2025 15:1155,729748,579,123720,484,0003,997,000323,584
24Nov 8, 2025 15:1255,799754,932,189722,391,0002,997,000323,584
25Nov 8, 2025 15:1857,744779,124,450750,674,0000315,392
26Nov 8, 2025 15:1159,581799,314,581771,555,0002,998,000270,336
27Nov 8, 2025 15:1264,245859,186,892835,183,0000516,096