Implement a Fractional Share Inventory with a Per-Ticker Buy Threshold
Company: Robinhood
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a system to manage fractional share inventory for a brokerage. The system processes buy and sell orders for fractional shares of stocks and keeps each ticker's inventory at or below a specified threshold.
When a buy order is placed, the system adds the quantity to the inventory for that ticker. If the buy would push the inventory above the threshold, the order is rejected. When a sell order is placed, the system removes the quantity. If the ticker holds less than that quantity, the sell is rejected.
The system also reports the current inventory level for any ticker and returns all tickers that currently have non-zero inventory, in lexicographic order.
Implement the following methods:
- `FractionalShareInventory(threshold: float)`: create an empty inventory with the given threshold.
- `buy(ticker: str, quantity: float) -> bool`: add the quantity to the ticker's inventory if the new level would not exceed the threshold. Return `True` if the order succeeds and `False` otherwise.
- `sell(ticker: str, quantity: float) -> bool`: remove the quantity if the ticker holds at least that many shares. Return `True` if the order succeeds and `False` otherwise.
- `getInventory(ticker: str) -> float`: return the current inventory level for the ticker, or `0.0` if it holds none.
- `getActiveTickers() -> List[str]`: return the tickers that currently have non-zero inventory, sorted lexicographically.
```hint Choose the number type first
Decide how each balance is stored before you write the checks. Most decimal fractions, such as 0.1, have no exact binary floating-point representation.
```
### Examples
Example 1:
```text
Input:
["FractionalShareInventory", "buy", "buy", "buy", "buy", "sell", "getInventory", "getActiveTickers"]
[[10], ["TSLA", 6.5], ["AAPL", 2.25], ["TSLA", 4.0], ["TSLA", 3.5], ["AAPL", 3.0], ["TSLA"], []]
Output: [null, true, true, false, true, false, 10.0, ["AAPL", "TSLA"]]
Explanation:
inv = FractionalShareInventory(10.0) # threshold = 10.0 per ticker
inv.buy("TSLA", 6.5) # -> True (TSLA: 6.5)
inv.buy("AAPL", 2.25) # -> True (AAPL: 2.25)
inv.buy("TSLA", 4.0) # -> False (6.5 + 4.0 = 10.5 exceeds 10.0)
inv.buy("TSLA", 3.5) # -> True (TSLA: 10.0, equal to the threshold is allowed)
inv.sell("AAPL", 3.0) # -> False (only 2.25 held)
inv.getInventory("TSLA") # -> 10.0
inv.getActiveTickers() # -> ["AAPL", "TSLA"]
```
Example 2:
```text
Input:
["FractionalShareInventory", "sell", "buy", "sell", "getInventory", "getActiveTickers"]
[[10], ["TSLA", 5], ["TSLA", 3.5], ["TSLA", 3.5], ["TSLA"], []]
Output: [null, false, true, true, 0.0, []]
Explanation:
inv = FractionalShareInventory(10.0) # threshold = 10.0 per ticker
inv.sell("TSLA", 5.0) # -> False (no inventory to sell)
inv.buy("TSLA", 3.5) # -> True (TSLA: 3.5)
inv.sell("TSLA", 3.5) # -> True (TSLA: 0.0, removed)
inv.getInventory("TSLA") # -> 0.0
inv.getActiveTickers() # -> []
```
### Constraints & Assumptions
- `1 <= threshold <= 10^6`
- Ticker symbols are non-empty strings of uppercase letters.
- `quantity > 0` for every order.
- At most `10^4` operations will be made.
- The threshold applies to each ticker separately. A buy that brings a ticker exactly to the threshold succeeds.
- A rejected order leaves the inventory unchanged.
- Quantities are exact decimal amounts: after buys of 0.7 and 0.1, a ticker holds 0.8 shares, and a sell of 0.8 succeeds.
### Clarifying Questions to Ask
- Is a rejected order rejected whole, or should it fill partially up to the threshold or up to the shares held?
- How many decimal places can a quantity carry in practice?
- Can orders arrive from several threads at once?
- How often is `getActiveTickers` called compared with `buy` and `sell`?
### What a Strong Answer Covers
- The structure that holds the balances and the cost of each method, including the sort behind `getActiveTickers`.
- The boundary cases: a buy that lands exactly on the threshold, a sell of exactly the held amount, and a sell for a ticker that was never bought.
- Whether a rejected order or a read of an unknown ticker can leave stray entries behind.
- How a ticker whose balance returns to zero stays out of the active list.
- How the balances avoid floating-point drift, and what precision the chosen representation supports.
### Follow-up Questions
- Two sells for the same ticker arrive on different threads at the same moment. What can go wrong, and how do you prevent it?
- If `getActiveTickers` is called thousands of times per second, what would you change?
- How would you persist the inventory so it survives a process restart?
- The threshold is lowered while some tickers hold more than the new limit. How should buys and sells behave for those tickers?
Overview: Implement a fractional share inventory that accepts buy orders up to a per-ticker threshold, rejects sells larger than the shares held, and lists active tickers in sorted order. It tests hash map design, boundary handling and keeping decimal share quantities exact under floating-point arithmetic.