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
Date AuthorLanguageStatus Score
Jun 8, 2026 08:07Victor MerckléVictor MerckléCPPclang++18.1.3Success1,904
Jun 7, 2026 09:15Victor MerckléVictor MerckléCPPclang++18.1.3Success1,591
Jun 7, 2026 09:05Victor MerckléVictor MerckléCPPclang++18.1.3Success1,552
Jun 7, 2026 08:58Victor MerckléVictor MerckléCPPclang++18.1.3Success1,542
Jun 7, 2026 08:50Victor MerckléVictor MerckléCPPclang++18.1.3Success1,507
Jun 7, 2026 08:42Victor MerckléVictor MerckléCPPclang++18.1.3Success1,788
Jun 7, 2026 04:06highloadLLMhighloadLLMCPPclang++20.1.2Success3,530
Jun 7, 2026 04:00highloadLLMhighloadLLMCPPclang++20.1.2Success3,511
Jun 7, 2026 03:49highloadLLMhighloadLLMCPPclang++20.1.2Success3,448
Jun 7, 2026 03:42highloadLLMhighloadLLMCPPclang++20.1.2Success7,477
Jun 7, 2026 03:32highloadLLMhighloadLLMCPPclang++20.1.2Success3,856
Jun 7, 2026 03:30anboto1anboto1CPPg++14.2.0Success20,539
Jun 7, 2026 03:26highloadLLMhighloadLLMCPPclang++20.1.2Success3,505
Jun 7, 2026 03:23anboto1anboto1CPPg++14.2.0Success20,626
Jun 7, 2026 03:19highloadLLMhighloadLLMCPPclang++20.1.2Success20,240
Jun 7, 2026 03:18anboto1anboto1CPPg++14.2.0Success43,534
Jun 7, 2026 03:12highloadLLMhighloadLLMCPPg++14.2.0Success3,912
Jun 7, 2026 03:09highloadLLMhighloadLLMCPPclang++20.1.2Success3,674
Jun 7, 2026 03:07anboto1anboto1CPPg++14.2.0Success21,058
Jun 7, 2026 03:05anboto1anboto1CPPg++14.2.0Success20,204
Jun 7, 2026 03:03highloadLLMhighloadLLMCPPclang++20.1.2Success3,517
Jun 7, 2026 03:00anboto1anboto1CPPg++14.2.0Success20,153
Jun 7, 2026 02:57highloadLLMhighloadLLMCPPclang++20.1.2Success3,434
Jun 7, 2026 02:55anboto1anboto1CPPg++14.2.0Success21,021
Jun 7, 2026 02:52highloadLLMhighloadLLMCPPclang++20.1.2Success3,386
Jun 7, 2026 02:52anboto1anboto1CPPg++10.5.0Success20,200
Jun 7, 2026 02:49anboto1anboto1CPPg++11.5.0Success20,953
Jun 7, 2026 02:47highloadLLMhighloadLLMCPPclang++20.1.2Success4,013
Jun 7, 2026 02:46anboto1anboto1CPPg++14.2.0Success21,244
Jun 7, 2026 02:41anboto1anboto1CPPg++14.2.0Success54,083
Jun 7, 2026 02:37highloadLLMhighloadLLMCPPclang++18.1.3Success3,461
Jun 7, 2026 02:36anboto1anboto1CPPg++14.2.0Success21,298
Jun 7, 2026 02:32highloadLLMhighloadLLMCPPclang++20.1.2Success3,436
Jun 7, 2026 02:26anboto1anboto1CPPg++14.2.0Success22,369
Jun 7, 2026 02:26highloadLLMhighloadLLMCPPclang++20.1.2Success3,424
Jun 7, 2026 02:20anboto1anboto1CPPg++14.2.0Success28,011
Jun 7, 2026 02:19highloadLLMhighloadLLMCPPclang++20.1.2Success3,384
Jun 7, 2026 02:15anboto1anboto1CPPg++14.2.0Success23,776
Jun 7, 2026 02:13anboto1anboto1CPPclang++18.1.3Success27,692
Jun 7, 2026 02:12highloadLLMhighloadLLMCPPclang++20.1.2Success3,504
Jun 7, 2026 02:10anboto1anboto1CPPg++14.2.0Success21,383
Jun 7, 2026 02:07highloadLLMhighloadLLMCPPclang++20.1.2Success3,383
Jun 7, 2026 02:06anboto1anboto1CPPg++14.2.0Success22,968
Jun 7, 2026 02:03anboto1anboto1CPPg++14.2.0Success41,258
Jun 7, 2026 02:02highloadLLMhighloadLLMCPPclang++20.1.2Success3,523
Jun 7, 2026 01:55anboto1anboto1CPPg++14.2.0Success29,152
Jun 7, 2026 01:55highloadLLMhighloadLLMCPPclang++20.1.2Success3,472
Jun 7, 2026 01:52anboto1anboto1CPPg++14.2.0Success20,909
Jun 7, 2026 01:49highloadLLMhighloadLLMCPPclang++20.1.2Success3,360+139.31 RP
Jun 7, 2026 01:49anboto1anboto1CPPg++14.2.0Success19,641+38.15 RP
Jun 7, 2026 01:46anboto1anboto1CPPg++12.4.0Success21,844
Jun 7, 2026 01:43highloadLLMhighloadLLMCPPclang++20.1.2Success3,583
Jun 7, 2026 01:42anboto1anboto1CPPg++13.3.0Success22,035
Jun 7, 2026 01:39highloadLLMhighloadLLMCPPclang++20.1.2Success3,555
Jun 7, 2026 01:37anboto1anboto1CPPg++14.2.0Success21,232+75.17 RP
Jun 7, 2026 01:32highloadLLMhighloadLLMCPPclang++20.1.2Success3,525+116.53 RP
Jun 7, 2026 01:32anboto1anboto1CPPclang++20.1.2Success50,699
Jun 7, 2026 01:26highloadLLMhighloadLLMCPPclang++20.1.2Success3,676+1'057.84 RP
Jun 7, 2026 01:23anboto1anboto1CPPclang++20.1.2Success26,899
Jun 7, 2026 01:21highloadLLMhighloadLLMCPPclang++20.1.2Success6,077
Jun 7, 2026 01:14anboto1anboto1CPPclang++20.1.2Success42,628
Jun 7, 2026 01:12highloadLLMhighloadLLMCPPclang++20.1.2Success6,015+1'306.87 RP
Jun 7, 2026 01:12anboto1anboto1CPPclang++20.1.2Success27,244
Jun 7, 2026 01:05anboto1anboto1CPPclang++20.1.2Success28,560
Jun 7, 2026 01:00highloadLLMhighloadLLMCPPclang++20.1.2Success29,787
Jun 7, 2026 00:58anboto1anboto1CPPclang++20.1.2Success25,264+39.39 RP
Jun 7, 2026 00:52anboto1anboto1CPPclang++18.1.3Success29,918
Jun 7, 2026 00:51highloadLLMhighloadLLMCPPclang++20.1.2Success29,329
Jun 7, 2026 00:48anboto1anboto1CPPclang++20.1.2Success30,972
Jun 7, 2026 00:46highloadLLMhighloadLLMCPPclang++20.1.2Success28,499
Jun 7, 2026 00:42anboto1anboto1CPPclang++20.1.2Success28,056+5.24 RP
Jun 7, 2026 00:39highloadLLMhighloadLLMCPPclang++20.1.2Success30,653
Jun 7, 2026 00:35anboto1anboto1CPPclang++20.1.2Success28,475+351.19 RP
Jun 7, 2026 00:33highloadLLMhighloadLLMCPPclang++20.1.2Success30,687
Jun 7, 2026 00:27highloadLLMhighloadLLMCPPclang++20.1.2Success30,277
Jun 7, 2026 00:25highloadLLMhighloadLLMCPPclang++18.1.3Success30,992
Jun 7, 2026 00:21highloadLLMhighloadLLMCPPclang++20.1.2Success31,395
Jun 7, 2026 00:13highloadLLMhighloadLLMCPPclang++20.1.2Success28,118+11.05 RP
Jun 7, 2026 00:09highloadLLMhighloadLLMCPPclang++20.1.2Success29,020+93.59 RP
Jun 6, 2026 23:58highloadLLMhighloadLLMCPPclang++20.1.2Success41,346
Jun 6, 2026 23:54highloadLLMhighloadLLMCPPclang++20.1.2Success41,752
Jun 6, 2026 23:51highloadLLMhighloadLLMCPPclang++20.1.2Success44,774
Jun 6, 2026 23:46highloadLLMhighloadLLMCPPclang++19.1.1Success40,706
Jun 6, 2026 23:42highloadLLMhighloadLLMCPPclang++20.1.2Success39,840+9.65 RP
Jun 6, 2026 23:38highloadLLMhighloadLLMCPPclang++20.1.2Success47,566
Jun 6, 2026 23:31highloadLLMhighloadLLMCPPclang++20.1.2Success41,433+1.72 RP
Jun 6, 2026 23:23highloadLLMhighloadLLMCPPclang++20.1.2Success43,996
Jun 6, 2026 23:17highloadLLMhighloadLLMCPPclang++20.1.2Success86,790
Jun 6, 2026 23:12highloadLLMhighloadLLMCPPclang++20.1.2Success41,730+36.23 RP
Jun 6, 2026 23:05highloadLLMhighloadLLMCPPclang++20.1.2Success51,686
Jun 6, 2026 23:02highloadLLMhighloadLLMCPPclang++20.1.2Success49,773
Jun 6, 2026 22:59highloadLLMhighloadLLMCPPclang++20.1.2Success49,162+7.88 RP
Jun 6, 2026 22:56highloadLLMhighloadLLMCPPclang++20.1.2Success51,144+2.41 RP
Jun 6, 2026 22:53highloadLLMhighloadLLMCPPclang++20.1.2Success55,988
Jun 6, 2026 22:50highloadLLMhighloadLLMCPPg++14.2.0Success55,544
Jun 6, 2026 22:48highloadLLMhighloadLLMCPPclang++20.1.2Success51,782+2.91 RP
Jun 6, 2026 22:45highloadLLMhighloadLLMCPPclang++18.1.3Success68,583
Jun 6, 2026 22:37highloadLLMhighloadLLMCPPclang++18.1.3Success58,479
Jun 6, 2026 22:34highloadLLMhighloadLLMCPPclang++18.1.3Success58,529
Jun 6, 2026 22:31highloadLLMhighloadLLMCPPclang++18.1.3Success52,575+23.70 RP