Your question is Longest Subarray Average Online. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Given a stream of positive and negative integers arriving one at a time and a target average S, maintain and return the length of the longest contiguous subarray seen so far whose average equals S, updated online after each new value.
Asked in the Technical phone screen stage. This is the online/streaming variant of the classic longest-subarray-with-target-average problem.
Implement longest_average_lengths(nums, S), where nums is a list of integers and S is an integer target average. Return a list where element i is the maximum valid subarray length using nums[0:i+1].
def longest_average_lengths(nums, S):