Design a live odds market board with sequenced updates, suspension and a favourite lookup
Company: BoyleSports
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an in-memory live odds market board fed by a streaming price feed. Each market, identified by `marketId`, contains selections identified by `selectionId`, each priced with decimal odds.
Updates carry a per-market `sequence` number. An update is applied only if its `sequence` is strictly greater than the last applied sequence for that market. Out-of-order or duplicate updates are ignored. The first update for a market is always applied.
Markets can be suspended and reopened. While a market is suspended, updates are still recorded (and sequence checks still apply), but price queries return `-1` and favourite queries return `""`. The favourite of a market is the selection with the lowest decimal odds, with ties broken by the lexicographically smallest `selectionId`. Suspending or reopening a market that has not been seen yet registers it with no selections in that state.
Implement a `MarketBoard` class with these methods:
- `updatePrice(marketId: str, selectionId: str, decimalOdds: float, sequence: int) -> bool` applies the price if `sequence` is strictly greater than the last applied sequence for the market (this works even while the market is suspended). It returns true if the update was applied and false if it was ignored as stale or duplicate.
- `suspendMarket(marketId: str)` suspends trading on the market. It registers the market if unknown and is idempotent.
- `reopenMarket(marketId: str)` reopens trading on the market. It registers the market if unknown and is idempotent.
- `getPrice(marketId: str, selectionId: str) -> float` returns the current decimal odds of the selection, or `-1` if the market or selection is unknown or the market is suspended.
- `getFavourite(marketId: str) -> str` returns the `selectionId` with the lowest decimal odds (ties broken by the lexicographically smallest id), or an empty string if the market is unknown, empty, or suspended.
Example 1 takes one market through a stale update, a tie, and a suspension:
```text
updatePrice("m1", "home", 2.5, 1) -> true
updatePrice("m1", "away", 3.0, 2) -> true
updatePrice("m1", "draw", 3.4, 2) -> false (2 is not greater than 2)
getFavourite("m1") -> "home"
updatePrice("m1", "away", 2.5, 5) -> true
getFavourite("m1") -> "away" (tie at 2.5, "away" < "home")
updatePrice("m1", "home", 1.9, 4) -> false (4 is not greater than 5)
suspendMarket("m1")
getPrice("m1", "away") -> -1
getFavourite("m1") -> ""
updatePrice("m1", "home", 2.1, 6) -> true (recorded while suspended)
reopenMarket("m1")
getPrice("m1", "home") -> 2.1
getFavourite("m1") -> "home"
```
The update at sequence 6 was applied while the market was suspended, so after reopening, home at 2.1 beats away at 2.5.
Example 2 covers unknown and empty markets:
```text
getPrice("x9", "a") -> -1
getFavourite("x9") -> ""
suspendMarket("x9")
reopenMarket("x9")
getFavourite("x9") -> ""
updatePrice("x9", "a", 1.5, 0) -> true
getFavourite("x9") -> "a"
```
Suspending `x9` registers it with no selections, so its favourite is still empty after it reopens. The first update is applied even though its sequence is 0.
```hint Finding the favourite quickly
Scanning every selection on each favourite query costs time linear in the market size. Think about which structure returns the smallest (odds, id) pair quickly, and how it stays correct after a selection is repriced.
```
```hint Where the sequence lives
The sequence belongs to the market, not to a selection. Decide what a newly registered market stores as its last sequence so that a first update with sequence 0 is still accepted.
```
### Constraints & Assumptions
- `1 <= marketId.length, selectionId.length <= 20`
- `marketId` and `selectionId` consist of lowercase English letters and digits.
- `1.01 <= decimalOdds <= 1000.0`
- `0 <= sequence <= 10^9`
- At most `10^5` calls in total will be made to all methods.
### Clarifying Questions to Ask
- Is the sequence number shared by all selections of a market, or tracked separately per selection?
- Can a selection ever be removed from a market, or is it only repriced?
- After a market reopens, should queries show prices that were applied while it was suspended?
- Is the board used from a single thread, or does the feed write while other threads query it?
- How many selections can one market hold, and how many updates can it receive over its lifetime?
### What a Strong Answer Covers
- The per-market state (prices, last applied sequence, suspension flag) and how an unknown market gets registered.
- The staleness rule applied per market, including updates that arrive during suspension and a first update at sequence 0.
- A favourite lookup faster than a full scan that applies the tie rule on `selectionId` and stays correct after reprices.
- The time and space cost of each method, including how the favourite structure grows under many reprices.
- Edge cases: unknown market or selection, an empty market, suspend or reopen on an unseen market, and a price that returns to an earlier value.
### Follow-up Questions
- The favourite structure gains an entry on every applied update. How would you bound its size for a market that is repriced millions of times?
- How would the design change if the feed thread writes while many reader threads query the board?
- What changes if sequence numbers are tracked per selection instead of per market?
- How would you return the three shortest-priced selections of a market?
Overview: Implement an in-memory live odds board that applies sequenced price updates per market, ignores stale and duplicate messages, and lets markets be suspended and reopened. It tests per-market state design, ordering rules, and a fast favourite lookup with a deterministic tie rule.