Betting Slip with Accumulator and Each-Way Returns

Quick Overview

Design a sportsbook betting slip that converts fractional odds to decimal odds, prices an accumulator as the product of its selections, and pays an each-way bet on a single selection with 1/4 or 1/5 place terms. It tests clean state handling, exact fraction arithmetic and half-up rounding of money.

Betting Slip with Accumulator and Each-Way Returns

Company: BoyleSports

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a betting slip for a sportsbook. Each selection is identified by a `selectionId` and quoted in fractional odds of the form `"N/D"` (for example `"5/2"`). The decimal odds of a selection are `N/D + 1`. Adding a selection whose id already exists replaces its odds. The slip supports an accumulator bet, whose odds are the product of the decimal odds of every selection (`1.0` when the slip is empty), and whose potential return is `stake * accumulator odds`. It also supports a two-part each-way bet on a slip holding exactly one selection (otherwise the result is `-1`). `stake` is the per-part stake. The place part always pays `stake * ((N/D) * placeFraction + 1)`, where `placeFraction` is `"1/4"` or `"1/5"`. The win part pays `stake * decimal odds` only when the selection won (`placed = false`). When `placed = true`, the selection placed but did not win, so only the place part pays. All returned money values are rounded to 2 decimal places, half up. Implement the following methods: - `addSelection(selectionId: str, fractionalOdds: str)`: add a selection quoted as `"N/D"`. If the id already exists, replace its odds. - `removeSelection(selectionId: str)`: remove the selection if present; otherwise do nothing. - `getDecimalOdds(selectionId: str) -> float`: return `N/D + 1` for the selection, or `-1` if it is not on the slip. - `getAccumulatorOdds() -> float`: return the product of all selections' decimal odds (`1.0` when the slip is empty). - `getPotentialReturn(stake: float) -> float`: return `stake * accumulator odds`, rounded to 2 decimal places. - `getEachWayReturn(stake: float, placeFraction: str, placed: bool) -> float`: return the total each-way payout on a single-selection slip, rounded to 2 decimals, or `-1` if the slip does not hold exactly one selection. `placed = true` pays only the place part; `placed = false` pays both parts. For example, with three selections: ```text slip = BetSlip() slip.addSelection("A", "5/2") # decimal odds 3.5 slip.addSelection("B", "1/1") # decimal odds 2.0 slip.addSelection("C", "1/4") # decimal odds 1.25 slip.getAccumulatorOdds() -> 8.75 slip.getPotentialReturn(10) -> 87.5 slip.getEachWayReturn(10, "1/4", false) -> -1 slip.removeSelection("B") slip.removeSelection("Z") # not on the slip, nothing happens slip.getAccumulatorOdds() -> 4.375 slip.getPotentialReturn(3) -> 13.13 ``` The accumulator odds are `3.5 * 2.0 * 1.25 = 8.75`. With three selections on the slip, an each-way bet is invalid. After B is removed the odds are `3.5 * 1.25 = 4.375`, and `3 * 4.375 = 13.125` rounds half up to 13.13. A single-selection slip whose odds are replaced: ```text slip = BetSlip() slip.getAccumulatorOdds() -> 1.0 slip.getPotentialReturn(5) -> 5.0 slip.getDecimalOdds("X") -> -1 slip.addSelection("X", "4/1") slip.addSelection("X", "6/4") # replaces 4/1 slip.getDecimalOdds("X") -> 2.5 slip.getEachWayReturn(4, "1/5", false) -> 15.2 slip.getEachWayReturn(4, "1/5", true) -> 5.2 ``` The empty slip has odds 1.0, so the return equals the stake. After the replacement X is quoted at 6/4, so its decimal odds are 2.5. The win part pays `4 * 2.5 = 10.0` and the place part pays `4 * (1.5 * 0.2 + 1) = 5.2`, for 15.2 in total. When the selection only placed, the payout is the place part alone. ```hint Keep the odds exact Parse "N/D" into two integers and keep them as a fraction. A value such as 5/3 is already slightly off once it becomes a float, and every product and money value built from it carries that error. ``` ```hint Test a half-cent case Put one selection at "1/2" on the slip and ask for the potential return on a stake of 0.15. The exact value is 0.225, so half up must give 0.23. Check what floor(x * 100 + 0.5) on floats returns. ``` ### Constraints & Assumptions - `1 <= N, D <= 1000` for every fractional odds string `"N/D"` - `placeFraction` is either `"1/4"` or `"1/5"` - `0 <= stake <= 10⁶` - `1 <= selectionId.length <= 20` - At most `20` selections are on the slip at any time - At most `10⁴` total method calls ### Clarifying Questions to Ask - Is `stake` the stake for each part, so an each-way bet costs twice that amount? - Should the odds values returned by `getDecimalOdds` and `getAccumulatorOdds` be rounded too, or only money values? - Must rounding be exact at half-cent values, or is ordinary floating-point precision acceptable? ### What a Strong Answer Covers - A selection store keyed by id that handles replacement, removal and the exactly-one-selection check cheaply. - Exact handling of fractional odds and money, so half-up rounding is correct at half-cent values. - The accumulator product and the potential return, including the empty slip. - The each-way rules: the single-selection guard, the per-part stake, place terms applied to the fractional odds, and which parts pay for each value of `placed`. - Time and space complexity per method. ### Follow-up Questions - How would you price system bets on the same slip, such as every double and every treble from four selections? - How would you extend each-way to an accumulator in which each leg has its own place terms? - If one selection in an accumulator is withdrawn before the event, how should the bet be settled? - If the odds change between adding a selection and placing the bet, how would the slip detect it and ask the user to accept the new price?

Overview: Design a sportsbook betting slip that converts fractional odds to decimal odds, prices an accumulator as the product of its selections, and pays an each-way bet on a single selection with 1/4 or 1/5 place terms. It tests clean state handling, exact fraction arithmetic and half-up rounding of money.

BoyleSports logo
BoyleSports
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a betting slip for a sportsbook. Each selection is identified by a selectionId and quoted in fractional odds of the form "N/D" (for example "5/2"). The decimal odds of a selection are N/D + 1. Adding a selection whose id already exists replaces its odds.

The slip supports an accumulator bet, whose odds are the product of the decimal odds of every selection (1.0 when the slip is empty), and whose potential return is stake * accumulator odds.

It also supports a two-part each-way bet on a slip holding exactly one selection (otherwise the result is -1). stake is the per-part stake. The place part always pays stake * ((N/D) * placeFraction + 1), where placeFraction is "1/4" or "1/5". The win part pays stake * decimal odds only when the selection won (placed = false). When placed = true, the selection placed but did not win, so only the place part pays. All returned money values are rounded to 2 decimal places, half up.

Implement the following methods:

  • addSelection(selectionId: str, fractionalOdds: str) : add a selection quoted as "N/D" . If the id already exists, replace its odds.
  • removeSelection(selectionId: str) : remove the selection if present; otherwise do nothing.
  • getDecimalOdds(selectionId: str) -> float : return N/D + 1 for the selection, or -1 if it is not on the slip.
  • getAccumulatorOdds() -> float : return the product of all selections' decimal odds ( 1.0 when the slip is empty).
  • getPotentialReturn(stake: float) -> float : return stake * accumulator odds , rounded to 2 decimal places.
  • getEachWayReturn(stake: float, placeFraction: str, placed: bool) -> float : return the total each-way payout on a single-selection slip, rounded to 2 decimals, or -1 if the slip does not hold exactly one selection. placed = true pays only the place part; placed = false pays both parts.

For example, with three selections:

slip = BetSlip()
slip.addSelection("A", "5/2")             # decimal odds 3.5
slip.addSelection("B", "1/1")             # decimal odds 2.0
slip.addSelection("C", "1/4")             # decimal odds 1.25
slip.getAccumulatorOdds()                 -> 8.75
slip.getPotentialReturn(10)               -> 87.5
slip.getEachWayReturn(10, "1/4", false)   -> -1
slip.removeSelection("B")
slip.removeSelection("Z")                 # not on the slip, nothing happens
slip.getAccumulatorOdds()                 -> 4.375
slip.getPotentialReturn(3)                -> 13.13

The accumulator odds are 3.5 * 2.0 * 1.25 = 8.75. With three selections on the slip, an each-way bet is invalid. After B is removed the odds are 3.5 * 1.25 = 4.375, and 3 * 4.375 = 13.125 rounds half up to 13.13.

A single-selection slip whose odds are replaced:

slip = BetSlip()
slip.getAccumulatorOdds()                 -> 1.0
slip.getPotentialReturn(5)                -> 5.0
slip.getDecimalOdds("X")                  -> -1
slip.addSelection("X", "4/1")
slip.addSelection("X", "6/4")             # replaces 4/1
slip.getDecimalOdds("X")                  -> 2.5
slip.getEachWayReturn(4, "1/5", false)    -> 15.2
slip.getEachWayReturn(4, "1/5", true)     -> 5.2

The empty slip has odds 1.0, so the return equals the stake. After the replacement X is quoted at 6/4, so its decimal odds are 2.5. The win part pays 4 * 2.5 = 10.0 and the place part pays 4 * (1.5 * 0.2 + 1) = 5.2, for 15.2 in total. When the selection only placed, the payout is the place part alone.

Constraints & Assumptions

  • 1 <= N, D <= 1000 for every fractional odds string "N/D"
  • placeFraction is either "1/4" or "1/5"
  • 0 <= stake <= 10⁶
  • 1 <= selectionId.length <= 20
  • At most 20 selections are on the slip at any time
  • At most 10⁴ total method calls

Clarifying Questions to Ask Guidance

  • Is stake the stake for each part, so an each-way bet costs twice that amount?
  • Should the odds values returned by getDecimalOdds and getAccumulatorOdds be rounded too, or only money values?
  • Must rounding be exact at half-cent values, or is ordinary floating-point precision acceptable?

What a Strong Answer Covers Guidance

  • A selection store keyed by id that handles replacement, removal and the exactly-one-selection check cheaply.
  • Exact handling of fractional odds and money, so half-up rounding is correct at half-cent values.
  • The accumulator product and the potential return, including the empty slip.
  • The each-way rules: the single-selection guard, the per-part stake, place terms applied to the fractional odds, and which parts pay for each value of placed .
  • Time and space complexity per method.

Follow-up Questions Guidance

  • How would you price system bets on the same slip, such as every double and every treble from four selections?
  • How would you extend each-way to an accumulator in which each leg has its own place terms?
  • If one selection in an accumulator is withdrawn before the event, how should the bet be settled?
  • If the odds change between adding a selection and placing the bet, how would the slip detect it and ask the user to accept the new price?