PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to reason about contiguous subarray sums, efficient array manipulation, and handling large numeric ranges within algorithmic constraints, and it falls under the Coding & Algorithms domain as a practical algorithmic implementation problem.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Determine if subarray sums to target

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an array of non-negative integers and a non-negative integer `target`. Determine whether there exists a **contiguous subarray** (continuous sequence of elements) whose sum is exactly equal to `target`. Return a boolean value (`true` or `false`). ### Function Signature (example) ```java boolean hasSubarrayWithSum(int[] numbers, int target) ``` ### Constraints - `1 ≤ numbers.length ≤ 10^5` - `0 ≤ numbers[i] ≤ 10^9` - `0 ≤ target ≤ 10^14` - The subarray must be contiguous (i.e., formed by `numbers[i] + numbers[i+1] + ... + numbers[j]`). ### Examples **Example 1** ```java int[] numbers1 = {1, 2, 3, 4, 5}; int target1 = 9; // Explanation: subarray [2, 3, 4] sums to 9 hasSubarrayWithSum(numbers1, target1) == true ``` **Example 2** ```java int[] numbers2 = {1, 3, 2, 5, 7, 2}; int target2 = 14; // Explanation: subarray [2, 5, 7] sums to 14 hasSubarrayWithSum(numbers2, target2) == true ``` **Example 3** ```java int[] numbers3 = {4, 3, 2, 7, 1, 2}; int target3 = 10; // Explanation: subarray [3, 2, 5] does not exist, but [3, 2, 5] is not in the array. // A valid subarray is [3, 2, 5] if present; here one valid subarray is [3, 2, 5] equivalent sum, but actually // for the given example assume there exists some contiguous subarray summing to 10, so return true. hasSubarrayWithSum(numbers3, target3) == true ``` **Example 4** ```java int[] numbers4 = {4, 3, 2, 7, 1, 2}; int target4 = 11; // There is no contiguous subarray that sums to 11 hasSubarrayWithSum(numbers4, target4) == false ```

Quick Answer: This question evaluates a candidate's ability to reason about contiguous subarray sums, efficient array manipulation, and handling large numeric ranges within algorithmic constraints, and it falls under the Coding & Algorithms domain as a practical algorithmic implementation problem.

Return whether a non-empty contiguous subarray of non-negative numbers sums to target.

Constraints

  • numbers are non-negative
  • subarray is non-empty

Examples

Input: ([1, 2, 3, 4, 5], 9)

Expected Output: True

Explanation: 2+3+4.

Input: ([1, 3, 2, 5, 7, 2], 14)

Expected Output: True

Explanation: 2+5+7.

Input: ([4, 3, 2, 7, 1, 2], 11)

Expected Output: False

Explanation: No matching subarray.

Input: ([1, 0, 2], 0)

Expected Output: True

Explanation: Single zero subarray.

Hints

  1. A sliding window works because all values are non-negative.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)