coding
Google
Microsoft
Meta

Google Coding Question: Online Longest Subarray with Average

Topics:
Prefix Sum
Hash Table
Two Pointers
Roles:
Machine Learning Engineer
Software Engineer
Data Engineer
Experience:
Entry Level
Mid Level
Senior

Question Description

You are given a stream of integers and a fixed target average S. After each new integer arrives you must report the length of the longest contiguous subarray within the prefix seen so far whose average equals S (or 0 if none exists).

Core idea: convert the average condition into a zero-difference condition on a transformed prefix-sum. Let pref[i] be the sum of the first i values. A subarray (i, j] has average S iff pref[j] - pref[i] = S * (j - i). Rearranging gives pref[j] - Sj = pref[i] - Si. Maintain the earliest index you see for each value of the transformed key (pref - S*index) in a hashmap; at each step compute the current key and look up the earliest index to update the maximum length. This yields an online O(n) time, O(n) space solution.

Flow in an interview: you should (1) explain the mathematical transform, (2) propose the online hashmap design, (3) discuss numeric precision and edge cases (non-integer S, floating-point error) and (4) analyze complexity and memory trade-offs.

Skill signals: you must demonstrate algebraic manipulation of averages, use of prefix sums, hash table design for online tracking, and handling of floating-point precision (use scaling, rational representation, or an epsilon). Also explain why sliding-window/two-pointers fail when inputs can be negative.

Common Follow-up Questions

  • How would you handle numerical precision if S is a non-terminating decimal? Explain using rational representation or fixed-point scaling.
  • Modify the tracker to also return the start and end indices of the longest qualifying subarray seen so far. How does that change your storage?
  • How would you adapt the solution if elements can be deleted from the front of the stream (a sliding-window stream) and you must still answer online?
  • What changes if the query is longest subarray with average >= S instead of exactly S? Describe an efficient approach or why it's harder.
  • How can you reduce memory when the stream is extremely long (near-infinite)? Discuss trade-offs and approximate/streaming alternatives.

Related Questions

1Longest subarray with sum equal to k (offline and online variants)
2Find longest zero-sum subarray using prefix sums and hashmap
3Sliding-window vs prefix-sum: when two-pointers work and when they don't
4Online algorithms for streaming subarray statistics (max average, median, sum)

Explore More Questions

Practice This Question with AI

Get real-time hints, detailed requirements, and insightful analysis of the question.

Google Coding: Online Longest Subarray with Target Average | Voker