Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Longest Subarray Sum Equals K

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

Your question is Longest Subarray Sum Equals 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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output