Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Largest Subarray With Sum k

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

Your question is Largest Subarray With Sum k. 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

A Citadel Securities analytics component receives a sequence of signed integer values, such as normalized market-data changes. Given an integer array nums and target k, return the length of the longest non-empty contiguous subarray whose elements sum exactly to k.

Return 0 if no such subarray exists.

Formal Specification

  • Input: nums, an array of integers, and k, an integer target.
  • Output: An integer representing the maximum length of a contiguous non-empty subarray with sum k.
  • Negative, zero, and positive values may all occur.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^14 <= k <= 10^14
  • The subarray must be non-empty

Function Signature

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