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
#AuthorScoreLanguageSolution
1Victor MerckléVictor Mercklé Grandmaster1,609
CPP
clang++18.1.3
May 5, 2026 09:39
40,605,870 ns · 19,817,000 ns · 1,106,000 ns · 2,379,776 B
2Josu San MartinJosu San Martin Grandmaster2,179
CPP
clang++18.1.3
Mar 20, 2026 21:16
53,639,015 ns · 25,296,000 ns · 3,035,000 ns · 2,494,464 B
3Yuriy LyfenkoYuriy Lyfenko Master2,295
CPP
clang++18.1.3
Mar 21, 2026 19:40
55,008,638 ns · 22,877,000 ns · 6,962,000 ns · 2,404,352 B
4yenw0dyenw0d Skilled3,091
RUST
rust-1.85.1
Dec 30, 2025 04:18
69,167,673 ns · 36,169,000 ns · 4,018,000 ns · 2,437,120 B
5olliecrowolliecrow Master4,888
RUST
rust-1.94.0
Apr 20, 2026 15:41
93,591,923 ns · 58,512,000 ns · 5,044,000 ns · 2,260,992 B
6Dominique GarmierDominique Garmier Master13,500
CPP
clang++18.1.3
Apr 24, 2026 07:43
194,904,675 ns · 171,490,000 ns · 4,011,000 ns · 2,318,336 B
7KeefeKeefe Expert14,466
CPP
g++13.3.0
Mar 13, 2026 18:39
211,468,580 ns · 186,062,000 ns · 2,000,000 ns · 2,424,832 B
8imotw2imotw2 Apprentice15,043
CPP
g++13.3.0
Oct 13, 2025 16:28
218,489,487 ns · 190,549,000 ns · 5,014,000 ns · 2,416,640 B
9Ludvig SjöströmLudvig Sjöström Apprentice16,707
CPP
g++13.3.0
Nov 1, 2025 00:02
242,420,315 ns · 214,190,000 ns · 3,002,000 ns · 2,347,008 B
10Carl BergmanCarl Bergman Novice24,109
RUST
rust-1.90.0
Mar 15, 2026 03:15
336,833,010 ns · 307,404,000 ns · 6,007,000 ns · 2,359,296 B
11Donald LeeDonald Lee Novice30,266
RUST
rust-1.95.0
Apr 23, 2026 20:25
411,863,628 ns · 386,480,000 ns · 6,990,000 ns · 2,506,752 B
12Charles CabergsCharles Cabergs Expert35,220
CPP
g++13.3.0
Apr 30, 2026 12:14
476,809,312 ns · 448,870,000 ns · 8,997,000 ns · 2,514,944 B
13lipapipalipapipa Skilled39,187
CPP
g++13.3.0
Feb 7, 2026 19:27
540,301,755 ns · 503,428,000 ns · 6,005,000 ns · 2,424,832 B
14idatsyidatsy Apprentice40,753
RUST
rust-1.92.0
Dec 20, 2025 12:07
553,024,924 ns · 515,801,000 ns · 13,994,000 ns · 2,531,328 B
15Aniruddha DebAniruddha Deb Master47,419
CPP
g++13.3.0
Dec 31, 2025 15:22
639,540,418 ns · 615,446,000 ns · 1,000,000 ns · 348,160 B
16JarnoJarno Novice68,724
RUST
rust-1.94.0
Mar 12, 2026 16:36
919,163,741 ns · 890,420,000 ns · 2,998,000 ns · 2,469,888 B
17izotoffizotoff Expert70,444
GO
go1.25.4
Nov 26, 2025 12:35
943,382,828 ns · 913,768,000 ns · 1,999,000 ns · 2,281,472 B
18WilmerFinleyWilmerFinley Novice98,971
CPP
g++13.3.0
Oct 13, 2025 06:43
1,313,910,752 ns · 1,225,642,000 ns · 60,982,000 ns · 2,486,272 B
19BenBen Expert115,528
CPP
g++14.2.0
Apr 20, 2026 19:09
1,530,628,736 ns · 1,491,882,000 ns · 9,992,000 ns · 2,342,912 B
20regnivonregnivon Novice131,491
RUST
rust-1.92.0
Dec 28, 2025 00:56
1,744,475,347 ns · 1,699,381,000 ns · 9,996,000 ns · 2,523,136 B
21TudyMTudyM Expert168,151
CPP
clang++20.1.2
Apr 27, 2026 22:00
2,201,811,631 ns · 2,170,974,000 ns · 14,992,000 ns · 2,412,544 B
22Oleg KovalovOleg Kovalov Skilled205,816
RUST
rust-1.90.0
Oct 25, 2025 10:50
2,701,179,433 ns · 2,667,617,000 ns · 7,995,000 ns · 2,453,504 B
23NoSIMD_C#NoSIMD_C# Skilled208,689
RUST
rust-1.93.1
Mar 3, 2026 13:26
2,746,965,552 ns · 2,699,967,000 ns · 12,995,000 ns · 2,416,640 B
24Daniel HarrisDaniel Harris Novice212,546
RUST
rust-1.90.0
Oct 5, 2025 16:52
2,791,406,829 ns · 2,756,108,000 ns · 6,995,000 ns · 2,285,568 B
25Enda CarrollEnda Carroll Novice233,148
RUST
rust-1.90.0
Oct 12, 2025 11:50
3,060,465,804 ns · 3,019,937,000 ns · 10,992,000 ns · 2,347,008 B
26Taylor DettlingTaylor Dettling Expert268,197
RUST
rust-1.95.0
Apr 21, 2026 01:30
3,508,359,224 ns · 3,472,580,000 ns · 13,990,000 ns · 2,375,680 B