Design a predictive battery management system that flags failing cells
Company: Tesla
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a predictive battery management system (BMS) that monitors individual battery cells. Every reading for a cell holds a voltage, a temperature and a current. The system stores the history of readings for each cell, answers queries for the latest reading and for averages, and predicts which cells need maintenance.
A cell is flagged for maintenance if any of these conditions holds:
1. The average voltage of the last 5 readings is below 3.0 V.
2. The average temperature of the last 5 readings is above 45.0 °C.
3. The average absolute current of the last 5 readings is above 10.0 A.
4. The voltage trend is declining: the average voltage of the last 3 readings is lower than the average voltage of the 3 readings before them.
If a cell has fewer than 5 readings, conditions 1 to 3 use all of its readings. Condition 4 requires at least 6 readings.
Implement a class `BatteryManagementSystem` with these methods:
```text
addReading(cellId: int, voltage: float, temperature: float, current: float) -> None
getLatestReading(cellId: int) -> List[float]
getCellAverage(cellId: int) -> List[float]
predictFailure(cellId: int) -> bool
```
- `addReading` adds a real-time reading for the given cell.
- `getLatestReading` returns the most recent reading for the cell as `[voltage, temperature, current]`, or `[-1.0, -1.0, -1.0]` if the cell has no readings.
- `getCellAverage` returns the average voltage, temperature and current across all readings for the cell as `[avgV, avgT, avgC]`, each rounded to 2 decimal places, or `[-1.0, -1.0, -1.0]` if the cell has no readings.
- `predictFailure` analyzes the cell's history and returns `True` if the cell is flagged for maintenance, `False` otherwise.
Example 1:
```text
addReading(1, 3.7, 25.0, 2.0)
addReading(1, 3.6, 26.0, -3.0)
getLatestReading(1) -> [3.6, 26.0, -3.0]
getCellAverage(1) -> [3.65, 25.5, -0.5]
predictFailure(1) -> False
getLatestReading(2) -> [-1.0, -1.0, -1.0]
```
Cell 1 has two readings, so every rule uses both. The average voltage is 3.65, the average temperature is 25.5 and the average absolute current is (2.0 + 3.0) / 2 = 2.5, all within limits, and two readings are too few for the trend rule. Cell 2 has never reported.
Example 2:
```text
addReading(3, 3.8, 40.0, 12.0)
addReading(3, 3.8, 40.0, -12.0)
getCellAverage(3) -> [3.8, 40.0, 0.0]
predictFailure(3) -> True
```
The signed currents cancel, so the average current is 0.0. Condition 3 averages absolute values, which gives 12.0, above the 10.0 A limit.
Example 3:
```text
addReading(7, 4.0, 30.0, 1.0)
addReading(7, 4.0, 30.0, 1.0)
addReading(7, 4.0, 30.0, 1.0)
addReading(7, 3.9, 30.0, 1.0)
addReading(7, 3.9, 30.0, 1.0)
predictFailure(7) -> False
addReading(7, 3.9, 30.0, 1.0)
predictFailure(7) -> True
getCellAverage(7) -> [3.95, 30.0, 1.0]
```
After five readings every average is within limits (the average voltage is 3.96) and the trend rule cannot run yet. The sixth reading enables it: the last three voltages average 3.9 and the three before them average 4.0, so the trend is declining. The average over all six voltages is 23.7 / 6 = 3.95.
```hint Count the work
Suppose most of the 10^5 calls hit one cell and alternate between addReading and getCellAverage. Estimate the total work of a straightforward implementation.
```
```hint Read the rules literally
Check each comparison for strictness, which quantity is averaged for current, and how many readings each rule needs before it applies.
```
### Constraints & Assumptions
- `1 <= cellId <= 10^4`
- `0.0 <= voltage <= 5.0`
- `-20.0 <= temperature <= 80.0`
- `-50.0 <= current <= 50.0`
- At most `10^5` calls are made in total across all methods.
- If a cell has fewer than 5 readings, conditions 1 to 3 use all available readings. Condition 4 requires at least 6 readings.
### Clarifying Questions to Ask
- Do readings for a cell always arrive in time order, or can a late reading carry an older timestamp?
- Should `getLatestReading` round its values the way `getCellAverage` does, or return them as stored?
- What should `predictFailure` return for a cell that has never reported?
- Does the system need to keep every reading forever, or only enough history to answer these four methods?
### What a Strong Answer Covers
- A per-cell structure that keeps readings in arrival order, with O(1) append and O(1) access to the newest reading.
- The cost of `getCellAverage` when one cell receives most of the calls, and a way to answer it in O(1).
- A `predictFailure` that reads only a fixed window of recent readings and gets the details right: fewer than 5 readings, the 6-reading minimum for the trend, strict comparisons, and the absolute value of current.
- Correct results for unknown cells, with rounding applied only where the prompt asks for it.
- How floating-point error behaves near the thresholds and at the rounding step.
### Follow-up Questions
- Each cell reports once per second for years. How do you bound memory while keeping all four methods correct?
- How would you make thresholds and window sizes configurable per battery chemistry, or add a fifth rule without editing `predictFailure`?
- Sensor threads call `addReading` concurrently for different cells. What do you lock, and at what granularity?
- How would you return every cell currently flagged for maintenance without scanning all cells?
Overview: Design a class that records voltage, temperature and current readings for battery cells, returns the latest reading and per-cell averages, and flags cells for maintenance with threshold and voltage-trend rules. It tests per-key state design, sliding-window checks, complexity under many calls, and careful edge-case handling.