Design a risk control kill switch for a trading engine

Quick Overview

Implement a kill switch class for a trading engine that tracks a signed cumulative position, checks trade prices against an inclusive range, and freezes permanently on the first breach. It tests careful state handling, boundary comparisons and clean object design.

Design a risk control kill switch for a trading engine

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design and implement a `RiskControlKillSwitch` for a trading engine that enforces real-time risk limits on position size and trade prices. The kill switch tracks a cumulative position (which can be updated by signed deltas) and accepts trades submitted at a given price. The engine becomes **permanently frozen** when either of the following risk breaches occurs: - The absolute value of the cumulative position strictly exceeds the configured `positionThreshold`. - A submitted trade price falls outside the inclusive range `[minPrice, maxPrice]`. Once frozen, the engine cannot be unfrozen programmatically. Any further `submit_trade` calls must be rejected (return `False`) without altering state, and `update_position` calls after freezing must also be ignored. The constructor takes `positionThreshold`, `minPrice` and `maxPrice`, and the position starts at 0. Implement the following methods: - `update_position(delta: int)`: Add delta to the cumulative position. If the absolute position then exceeds `positionThreshold`, freeze the engine. No-op if already frozen. - `submit_trade(price: int) -> bool`: Attempt to submit a trade at the given price. Returns `False` if the engine is frozen or if the price is outside `[minPrice, maxPrice]` (which also freezes the engine). Returns `True` on success. - `is_frozen() -> bool`: Return `True` if the engine is currently frozen, otherwise `False`. In the first example, the threshold is 100 and the price range is [10, 50]. The second position update pushes the position past the threshold, so the later trade is rejected. ```text Input: ["RiskControlKillSwitch", "update_position", "submit_trade", "is_frozen", "update_position", "is_frozen", "submit_trade", "is_frozen"] [[100, 10, 50], [40], [25], [], [70], [], [30], []] Output: [null, null, true, false, null, true, false, true] ks = RiskControlKillSwitch(100, 10, 50) # threshold=100, price range [10, 50] ks.update_position(40) # position = 40 (within ±100) ks.submit_trade(25) # -> True (price 25 in range) ks.is_frozen() # -> False (no breach yet) ks.update_position(70) # position = 110, |110| > 100 -> freeze ks.is_frozen() # -> True ks.submit_trade(30) # -> False (rejected, engine frozen) ks.is_frozen() # -> True ``` In the second example, a trade priced above the range freezes the engine, and a later trade inside the range is still rejected. ```text Input: ["RiskControlKillSwitch", "submit_trade", "submit_trade", "is_frozen", "submit_trade", "is_frozen"] [[1000, 100, 200], [150], [205], [], [180], []] Output: [null, true, false, true, false, true] ks = RiskControlKillSwitch(1000, 100, 200) # threshold=1000, price range [100, 200] ks.submit_trade(150) # -> True (in range) ks.submit_trade(205) # -> False (price > 200 -> freeze) ks.is_frozen() # -> True ks.submit_trade(180) # -> False (rejected, engine frozen) ks.is_frozen() # -> True ``` ```hint Order and boundaries Decide what each method does first when the engine is already frozen, so that a rejected call leaves every field as it was. Then check each comparison against the spec: which limit is strict and which is inclusive? ``` ### Constraints & Assumptions - `1 <= positionThreshold <= 10^9` - `0 <= minPrice <= maxPrice <= 10^9` - `-10^9 <= delta <= 10^9` for `update_position` - `0 <= price <= 10^9` for `submit_trade` - At most `10^5` calls in total across all methods. ### Clarifying Questions to Ask - Does an accepted trade change the cumulative position, or does the position move only through `update_position`? - Will the methods be called from a single thread, or can several threads share one instance? - Should the switch remember which limit caused the freeze, or is the boolean state enough? ### What a Strong Answer Covers - The state the class keeps and why a fixed set of fields is enough. - How a frozen engine ignores later updates and trades without changing position or flag. - The boundary cases: a position exactly at the threshold, a negative position, and prices equal to the range ends. - What happens to the trade that breaches the price range in the same call that freezes the engine. - Time and space per operation, and the integer width needed for the position in a fixed-width language. ### Follow-up Questions - How would you make the switch safe when several order-handling threads call it at once? - If accepted trades should also move the position, how would `submit_trade` change, and should a trade that would breach the limit be rejected before it is sent? - How would you record why and when the engine froze, and what would a controlled manual reset by an operator look like? - How would you extend the design to per-symbol position limits?

Overview: Implement a kill switch class for a trading engine that tracks a signed cumulative position, checks trade prices against an inclusive range, and freezes permanently on the first breach. It tests careful state handling, boundary comparisons and clean object design.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design and implement a RiskControlKillSwitch for a trading engine that enforces real-time risk limits on position size and trade prices.

The kill switch tracks a cumulative position (which can be updated by signed deltas) and accepts trades submitted at a given price. The engine becomes permanently frozen when either of the following risk breaches occurs:

  • The absolute value of the cumulative position strictly exceeds the configured positionThreshold .
  • A submitted trade price falls outside the inclusive range [minPrice, maxPrice] .

Once frozen, the engine cannot be unfrozen programmatically. Any further submit_trade calls must be rejected (return False) without altering state, and update_position calls after freezing must also be ignored.

The constructor takes positionThreshold, minPrice and maxPrice, and the position starts at 0. Implement the following methods:

  • update_position(delta: int) : Add delta to the cumulative position. If the absolute position then exceeds positionThreshold , freeze the engine. No-op if already frozen.
  • submit_trade(price: int) -> bool : Attempt to submit a trade at the given price. Returns False if the engine is frozen or if the price is outside [minPrice, maxPrice] (which also freezes the engine). Returns True on success.
  • is_frozen() -> bool : Return True if the engine is currently frozen, otherwise False .

In the first example, the threshold is 100 and the price range is [10, 50]. The second position update pushes the position past the threshold, so the later trade is rejected.

Input:
["RiskControlKillSwitch", "update_position", "submit_trade", "is_frozen", "update_position", "is_frozen", "submit_trade", "is_frozen"]
[[100, 10, 50],           [40],              [25],           [],          [70],              [],          [30],           []]

Output: [null, null, true, false, null, true, false, true]

ks = RiskControlKillSwitch(100, 10, 50)   # threshold=100, price range [10, 50]
ks.update_position(40)                    # position = 40 (within ±100)
ks.submit_trade(25)                       # -> True   (price 25 in range)
ks.is_frozen()                            # -> False  (no breach yet)
ks.update_position(70)                    # position = 110, |110| > 100 -> freeze
ks.is_frozen()                            # -> True
ks.submit_trade(30)                       # -> False  (rejected, engine frozen)
ks.is_frozen()                            # -> True

In the second example, a trade priced above the range freezes the engine, and a later trade inside the range is still rejected.

Input:
["RiskControlKillSwitch", "submit_trade", "submit_trade", "is_frozen", "submit_trade", "is_frozen"]
[[1000, 100, 200],        [150],          [205],          [],          [180],          []]

Output: [null, true, false, true, false, true]

ks = RiskControlKillSwitch(1000, 100, 200) # threshold=1000, price range [100, 200]
ks.submit_trade(150)                       # -> True   (in range)
ks.submit_trade(205)                       # -> False  (price > 200 -> freeze)
ks.is_frozen()                             # -> True
ks.submit_trade(180)                       # -> False  (rejected, engine frozen)
ks.is_frozen()                             # -> True

Constraints & Assumptions

  • 1 <= positionThreshold <= 10^9
  • 0 <= minPrice <= maxPrice <= 10^9
  • -10^9 <= delta <= 10^9 for update_position
  • 0 <= price <= 10^9 for submit_trade
  • At most 10^5 calls in total across all methods.

Clarifying Questions to Ask Guidance

  • Does an accepted trade change the cumulative position, or does the position move only through update_position ?
  • Will the methods be called from a single thread, or can several threads share one instance?
  • Should the switch remember which limit caused the freeze, or is the boolean state enough?

What a Strong Answer Covers Guidance

  • The state the class keeps and why a fixed set of fields is enough.
  • How a frozen engine ignores later updates and trades without changing position or flag.
  • The boundary cases: a position exactly at the threshold, a negative position, and prices equal to the range ends.
  • What happens to the trade that breaches the price range in the same call that freezes the engine.
  • Time and space per operation, and the integer width needed for the position in a fixed-width language.

Follow-up Questions Guidance

  • How would you make the switch safe when several order-handling threads call it at once?
  • If accepted trades should also move the position, how would submit_trade change, and should a trade that would breach the limit be rejected before it is sent?
  • How would you record why and when the engine froze, and what would a controlled manual reset by an operator look like?
  • How would you extend the design to per-symbol position limits?