Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Longest Subarray Sum Equals K
00:00
5 left

Longest Subarray Sum Equals K

MediumPython

Problem

Given an array of integers nums and an integer k, return the length of the longest contiguous subarray whose elements sum to k.

If no such subarray exists, return 0.

Formal Specification

  • Input:
    • nums: a list of integers
    • k: an integer target sum
  • Output:
    • An integer representing the maximum length of any contiguous subarray with sum exactly k

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^14 <= k <= 10^14
  • The answer fits in a 32-bit signed integer

Function Signature

def longest_subarray_sum_k(nums, k):
Interviewer

Your question is Longest Subarray Sum Equals K. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.