Compute rolling means without spreading non-finite values

Quick Overview

Compute contiguous moving averages in linear time and keep NaN or infinite inputs local to affected windows. Define non-finite semantics, restore finite outputs after exceptional values leave, and discuss numerical error.

Compute rolling means without spreading non-finite values

Company: Headlands

Role: Quantitative Researcher

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

Given a vector of floating-point values and a window size k, return the mean of each contiguous length-k window in better than O(nk) time. Extend the function so that non-finite values affect only windows containing them. Later finite windows must recover. ```hint Choose the comparison Consider which information must leave the accumulator when NaN or an infinity leaves the window. ``` ### Clarifying Questions - Should a window containing NaN, or both positive and negative infinity, return NaN? - Should a window with just one sign of infinity return that infinity? - Can finite-value magnitudes and window sizes be bounded to avoid finite-accumulator overflow? ### What a Strong Answer Covers - A linear-time rolling update - Explicit NaN and infinity semantics with recovery - Boundary validation and floating-point limitations ### Follow-up Questions - Why does subtracting a departing NaN not repair an ordinary running sum? - How would you reduce round-off error over a very long stream?

Overview: Compute contiguous moving averages in linear time and keep NaN or infinite inputs local to affected windows. Define non-finite semantics, restore finite outputs after exceptional values leave, and discuss numerical error.

Read the full Headlands Quantitative Researcher interview experience this question came from

Headlands logo
Headlands
Mar 9, 2025
hardQuantitative ResearcherTechnical ScreenSoftware Engineering Fundamentals
0
0

Given a vector of floating-point values and a window size k, return the mean of each contiguous length-k window in better than O(nk) time. Extend the function so that non-finite values affect only windows containing them. Later finite windows must recover.

Clarifying Questions Guidance

  • Should a window containing NaN, or both positive and negative infinity, return NaN?
  • Should a window with just one sign of infinity return that infinity?
  • Can finite-value magnitudes and window sizes be bounded to avoid finite-accumulator overflow?

What a Strong Answer Covers Guidance

  • A linear-time rolling update
  • Explicit NaN and infinity semantics with recovery
  • Boundary validation and floating-point limitations

Follow-up Questions Guidance

  • Why does subtracting a departing NaN not repair an ordinary running sum?
  • How would you reduce round-off error over a very long stream?