Solve Subarray Sum and Local Minimum
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
Quick Answer: This question evaluates array algorithm proficiency and complexity analysis by testing the ability to count target-sum contiguous subarrays and to locate a local minimum under a logarithmic-time constraint. It is commonly asked in the Coding & Algorithms domain to measure practical implementation skills and conceptual understanding of algorithmic patterns and time-complexity guarantees.
Part 1: Count Target-Sum Subarrays
Constraints
- 0 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- -10^9 <= k <= 10^9
Examples
Input: ([1, 1, 1], 2)
Expected Output:
Explanation: The subarrays [1, 1] at indices (0..1) and (1..2) both sum to 2.
Input: ([1, 2, 3], 3)
Expected Output:
Explanation: The valid subarrays are [1, 2] and [3].
Input: ([3, 4, 7, 2, -3, 1, 4, 2], 7)
Expected Output:
Explanation: There are four valid subarrays whose sum is 7.
Input: ([], 0)
Expected Output:
Explanation: An empty array has no subarrays.
Input: ([1, -1, 0], 0)
Expected Output:
Explanation: The valid subarrays are [1, -1], [0], and [1, -1, 0].
Hints
- Think about prefix sums: if prefix_sum[j] - prefix_sum[i] == k, then the subarray (i+1..j) has sum k.