Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Array Sum by Value

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

Your question is Array Sum by Value. 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

The City of Providence processes ordered event values such as permit adjustments or service-request time changes. Given an integer array and a target value, analyze every non-empty contiguous subarray whose elements sum exactly to the target.

Implement a function that returns the number of matching subarrays, the shortest matching length, and the longest matching length. Array values may be negative, zero, or positive, so a sliding-window approach is not valid.

Formal Specification

Implement analyze_target_subarrays(nums, target).

  • Input: nums, a non-empty array of integers, and target, an integer.
  • Output: A dictionary with three integer fields:
    • count: number of contiguous subarrays with sum equal to target.
    • shortest: minimum length among matching subarrays, or 0 if none exist.
    • longest: maximum length among matching subarrays, or 0 if none exist.
  • Each array position may be used by multiple matching subarrays.

Constraints

  • 1 <= len(nums) <= 200,000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • A matching subarray must be non-empty
  • Prefix sums may require 64-bit-safe arithmetic

Function Signature

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