Solve sliding window and tree BFS problems
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving and data-structure manipulation, focusing on the sliding window technique for array subproblems and iterative breadth-first search for binary tree level-order traversal, with emphasis on time and space complexity reasoning.
Minimum Size Subarray Sum
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^4
- 1 <= target <= 10^9
- All elements of nums are positive integers
Examples
Input: ([2,3,1,2,4,3], 7)
Expected Output: 2
Explanation: The subarray [4,3] sums to 7 (>= 7) with length 2, the shortest such window.
Input: ([1,4,4], 4)
Expected Output: 1
Explanation: A single element [4] already meets the target, so length 1.
Input: ([1,1,1,1,1,1,1,1], 11)
Expected Output: 0
Explanation: Total sum is 8 < 11; no subarray qualifies, so return 0.
Input: ([5], 5)
Expected Output: 1
Explanation: The single element equals the target, length 1.
Input: ([], 5)
Expected Output: 0
Explanation: Empty array — no subarray exists, return 0.
Input: ([1,2,3,4,5], 15)
Expected Output: 5
Explanation: Only the entire array sums to 15, so the minimal length is 5.
Input: ([1,2,3,4,5], 11)
Expected Output: 3
Explanation: [3,4,5] sums to 12 (>= 11) with length 3, shorter than any other valid window.
Hints
- A window of positive integers only grows when you extend the right edge and only shrinks when you advance the left edge — sums are monotonic, which is what makes the two-pointer sweep valid.
- Keep a running sum. Whenever it reaches `target`, record the current window length, then shrink from the left to look for an even smaller valid window before continuing to expand.
- Track the best length in a variable initialized to a sentinel (e.g. n+1). If it never improves, the answer is 0.
Binary Tree Level Order Traversal
Constraints
- The number of nodes is in the range [0, 2000]
- -1000 <= Node.val <= 1000
- The input array is a valid level-order encoding of a binary tree with None/null marking missing children
Examples
Input: ([3, 9, 20, None, None, 15, 7],)
Expected Output: [[3], [9, 20], [15, 7]]
Explanation: Root 3 at level 0; its children 9 and 20 at level 1; 20's children 15 and 7 at level 2.
Input: ([1],)
Expected Output: [[1]]
Explanation: Single node forms one level.
Input: ([],)
Expected Output: []
Explanation: Empty tree returns an empty list.
Input: ([1, 2, 3, 4, None, None, 5],)
Expected Output: [[1], [2, 3], [4, 5]]
Explanation: Level 0: [1]; level 1: [2,3]; level 2: 2's left child 4 and 3's right child 5.
Input: ([1, None, 2, None, 3],)
Expected Output: [[1], [2], [3]]
Explanation: A right-skewed chain 1 -> 2 -> 3 yields one node per level.
Input: ([5, -3, 8],)
Expected Output: [[5], [-3, 8]]
Explanation: Negative values are handled normally; root 5, children -3 and 8.
Hints
- Use a queue seeded with the root. The key to grouping by level is to capture the queue's size at the start of each iteration and process exactly that many nodes before moving on.
- For each node you dequeue, append its value to the current level's list and enqueue its non-null children (left before right) to be processed in the next round.
- Handle the empty-tree case up front: if the root is missing, return an empty list.