Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Longest Subarray Average Online

HardPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Input and Output

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].

Constraints

  • 1 <= len(nums) <= 1000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= S <= 10^9
  • All input values are integers

Function Signature

def longest_average_lengths(nums, S):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output