Bet Placement Ledger with Idempotent Settlement
Company: BoyleSports
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a betting ledger that tracks user balances, accepts bets and settles them. Each bet has a unique `betId`, belongs to a `userId`, is placed on a `selectionId`, and has a `stake` and `decimalOdds`. A bet is accepted only if the user's balance covers the stake, at which point the stake is deducted. Otherwise the bet is recorded as rejected.
Placement is idempotent: re-submitting an existing `betId` changes nothing and reports whether the original bet was accepted. Settlement is exactly once: a `"won"` bet credits `stake * decimalOdds`, a `"lost"` bet credits nothing, and a `"void"` bet refunds the stake. Settling an unknown, rejected or already settled bet does nothing and returns `false`.
The ledger also reports the liability of a selection: the total potential payout (`stake * decimalOdds`) of all accepted bets on it that are not yet settled. All money is rounded to 2 decimal places, and a potential payout is rounded half up to the nearest cent. Unknown users have a balance of `0`.
Implement the following methods:
- `deposit(userId: str, amount: float)`: add `amount` to the user's balance, creating the user if needed.
- `placeBet(betId: str, userId: str, selectionId: str, stake: float, decimalOdds: float) -> bool`: if `betId` is new, accept the bet and deduct the stake when the balance covers it (return `true`), otherwise record it as rejected (return `false`). If `betId` already exists, change nothing and return whether the original bet was accepted.
- `settleBet(betId: str, result: str) -> bool`: settle an accepted, unsettled bet. `"won"` credits `stake * decimalOdds`, `"lost"` credits nothing and `"void"` refunds the stake. Return `true` on the first settlement and `false` if the bet is unknown, rejected or already settled.
- `getBalance(userId: str) -> float`: return the user's balance rounded to 2 decimals (`0.0` for unknown users).
- `getLiability(selectionId: str) -> float`: return the total potential payout of unsettled accepted bets on the selection, rounded to 2 decimals.
For example, on a new ledger:
```text
deposit("u1", 100.00)
placeBet("b1", "u1", "s1", 40.00, 2.50) -> true
placeBet("b2", "u1", "s1", 70.00, 1.80) -> false
placeBet("b1", "u1", "s2", 5.00, 3.00) -> true
getLiability("s1") -> 100.0
getLiability("s2") -> 0.0
settleBet("b1", "won") -> true
settleBet("b1", "won") -> false
settleBet("b2", "void") -> false
getBalance("u1") -> 160.0
getLiability("s1") -> 0.0
```
Bet b1 is accepted and leaves 60.00 in the account, so b2 (stake 70.00) is rejected. The second b1 call is a retry: it returns the original result and does not create a bet on s2. Winning b1 pays `40.00 * 2.50 = 100.00`, so the balance becomes 160.00. The repeated settlement and the settlement of the rejected b2 both return `false`.
A second sequence shows the rounding rule and a retried rejected bet:
```text
deposit("u2", 10.00)
placeBet("b3", "u2", "s9", 0.15, 1.50) -> true
placeBet("b4", "u2", "s9", 12.00, 2.00) -> false
deposit("u2", 5.00)
placeBet("b4", "u2", "s9", 12.00, 2.00) -> false
getLiability("s9") -> 0.23
settleBet("b3", "void") -> true
getBalance("u2") -> 15.0
getBalance("nobody") -> 0.0
```
The potential payout of b3 is exactly 0.225, which rounds half up to 0.23. Bet b4 was rejected when the balance was 9.85, and the retry after the deposit still returns `false` because the original bet was rejected. Voiding b3 refunds 0.15, so the balance is `9.85 + 5.00 + 0.15 = 15.00`.
```hint Representing money
Picture a balance after thousands of float deposits and credits, and work out what a payout of 0.15 at odds 1.50 must round to. Choose an internal representation in which "half up to the cent" is exact.
```
```hint What a bet record needs
Idempotent placement needs a record for every betId you have seen, including the rejected ones. Decide which fields let settlement validate the bet and update liability without scanning other bets.
```
### Constraints & Assumptions
- `0 < amount <= 10⁶`
- `0 < stake <= 10⁶`
- `1.0 <= decimalOdds <= 1000.0`
- `amount`, `stake` and `decimalOdds` have at most 2 decimal places
- `result` is one of `"won"`, `"lost"`, `"void"`
- `userId`, `betId` and `selectionId` are non-empty strings
- At most `10⁴` total method calls
### Clarifying Questions to Ask
- If a `betId` is resubmitted with a different stake, odds or selection, is it still treated as the original bet?
- Is a selection's liability the sum of each bet's payout rounded to the cent, or the exact sum rounded once at the end?
- Does a balance exactly equal to the stake count as covering it?
- Will calls arrive from a single thread, or must placement and settlement be safe under concurrency?
### What a Strong Answer Covers
- A money representation that keeps repeated additions exact and makes the half-up payout rounding correct.
- Idempotent placement for both accepted and rejected betIds, including retries that carry different arguments.
- Guards that let each accepted bet settle at most once, with the correct credit for won, lost and void.
- Per-selection liability kept current on placement and settlement without scanning bets, and consistent with what a won bet pays.
- Time and space complexity per operation, plus edge cases such as unknown users and a balance equal to the stake.
### Follow-up Questions
- How would you make placement and settlement safe when they arrive concurrently, first within one process and then across several service instances?
- A settled result is sometimes corrected, for example a won bet later ruled void. How would you support resettlement and keep an audit trail?
- How would you persist the ledger so that a crash between debiting the balance and recording the bet cannot lose or double-count money?
- How would you add a per-selection liability cap that rejects bets which would push the selection above it?
Overview: Implement a betting ledger that accepts a bet only when the balance covers the stake, returns the original outcome when a bet ID is resubmitted, and settles each accepted bet exactly once as won, lost or void. It tests state modeling, exact money arithmetic with half-up rounding, and per-selection liability tracking.