The Find Two Non-overlapping Sub-arrays Each With Target Sum interview question is a complex optimization problem. You are given an integer array and a target value. You need to find two non-overlapping contiguous subarrays that each sum up to target. Among all such pairs of subarrays, you want to find the one that minimizes the total sum of their lengths. If no such pair exists, return -1.
Google ask the Find Two Non-overlapping Sub-arrays coding problem to test a candidate's mastery of Sliding Window and Dynamic Programming. It’s a multi-layered problem where you must first identify all valid subarrays and then choose the best two without intersection. It evaluations your ability to maintain "best so far" statistics while scanning an array.
This problem follows the Sliding Window and Prefix Minimum pattern.
target.minLen[i] representing the minimum length of a valid subarray found in the range nums[0...i].nums[left...right] with sum target:
left using minLen[left-1].minLen[right] for future windows.nums = [3, 2, 2, 4, 3], target = 3.
[3] at index 0 (len 1) and [3] at index 4 (len 1).[3]. minLen[0] = 1.[3]. Valid previous minLen before index 4 exists? Yes, at index 0.minLen array, which should store the minimum length seen anywhere in the prefix, not just ending at .When asked for "two non-overlapping" items, think about splitting the array at index . Find the "best" item to the left of and the "best" item to the right of . This Dynamic Programming interview pattern is a standard way to handle non-overlap constraints.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find the Longest Equal Subarray | Medium | Solve | |
| Longest Arithmetic Subsequence | Medium | Solve | |
| Minimum Number of Operations to Make Array Continuous | Hard | Solve | |
| Find the Median of the Uniqueness Array | Hard | Solve | |
| Longest Square Streak in an Array | Medium | Solve |