C++ Sliding-Window Mean of a Float Array With Inf, -Inf and NaN Handling
Company: Headlands
Role: Data Scientist
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Online Assessment
In C++, given an array `arr` of `float` values and a fixed window size `k`, compute the mean of every contiguous window of length `k`. The input may contain `inf`, `-inf`, and `NaN`, and the implementation must handle them correctly.
A possible interface is:
```cpp
std::vector<float> sliding_mean(const std::vector<float>& arr, std::size_t k);
```
```hint Watch what leaves the window
A running sum adds the entering element and subtracts the leaving one. Check what that subtraction does when the leaving element is infinite or NaN.
```
```hint Look beyond special values
Even with all-finite input, think about what a long-running `float` accumulator does to range and accuracy.
```
### Constraints and Clarifications
- For `n = arr.size()` and `1 <= k <= n`, return `n - k + 1` means in window order. The first mean covers `arr[0..k-1]`.
- Unless the interviewer specifies otherwise, treat non-finite inputs the way IEEE-754 arithmetic would treat them in the window's sum. The mean of an all-finite window should be the true mean rounded to `float`, even if an intermediate `float` sum would overflow.
- Aim for O(n) total time rather than recomputing every window from scratch.
### Clarifying Questions
- What should `k == 0` or `k > n` produce: an empty result or an error?
- Should NaN propagate to every window that contains it, or be skipped so the mean covers the remaining values?
- Is the output `float` or `double`, and how close must it be to an exact per-window computation?
- Might the code be compiled with aggressive floating-point optimization flags?
### What a Strong Answer Covers
- Correct results for every combination of NaN, `inf`, `-inf`, and finite values within a window.
- An O(n) algorithm whose state cannot be permanently corrupted by one non-finite value.
- Numerical robustness for finite values: accumulator type, overflow, cancellation, and drift over long inputs.
- Correct use of standard C++ facilities for classifying floating-point values, and awareness of compiler settings that break them.
- Tests that cover window boundaries and each special value entering and leaving the window.
### Follow-up Questions
1. How would you compute a sliding-window variance with the same non-finite handling?
2. How would your approach change if the values arrived as an unbounded stream?
3. Why can NaN checks stop working under aggressive floating-point optimization, and how would you guard against that?
Overview: Compute the mean of every fixed-size sliding window over a C++ float array that may contain inf, -inf and NaN. It tests IEEE-754 semantics for special values, an O(n) running computation that one bad value cannot permanently corrupt, and numerical robustness against overflow, cancellation and aggressive compiler flags.
Read the full Headlands Data Scientist interview experience this question came from