Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Target Sum Subarray

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

Your question is Target Sum Subarray. 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

Saviynt Identity Cloud may process ordered integer deltas representing changes in access assignments. Given an array of integers and a target value, find the longest non-empty contiguous subarray whose elements sum exactly to the target.

Formal Specification

Implement longest_subarray_sum(nums, target). The parameter nums is a list of integers, and target is an integer. Return a two-element list [start, end] containing the zero-based inclusive indices of the longest qualifying subarray. Return [] if no non-empty subarray has the target sum. If multiple subarrays have the same maximum length, return the one with the smallest starting index.

Values may be positive, zero, or negative, so a sliding-window technique is not generally valid.

Constraints

  • 1 <= len(nums) <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • -10^14 <= target <= 10^14
  • The subarray must be non-empty

Function Signature

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