← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Meta phone screen for a SWE role, two problems back to back. The second one was this prefix-sum subarray count question which sounds easy until you've been burned by the sliding-window trap before.

Questions Asked (1)

Q1

Given an integer array and a target value k, return the count of contiguous subarrays whose sum equals k. The array may contain negative numbers.

Algorithms & Data Structures
Author's notes

The negative values thing is what kills people.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a hash map to store prefix sums and their frequencies. Iterate through the array, maintaining a running sum, and for each element check if (current_sum - k) exists in the map; if so, add its frequency to the count. Then update the map with the current sum. This handles negative numbers and runs in O(n) time.

Pro tip: Clarify that the array can contain negative numbers, which rules out sliding window; this shows you understand the problem constraints. Also, mention that the hash map approach is optimal and explain why it works with an example.

1. Clarify the problem

Confirm that the array can have negative numbers and that we need to count all contiguous subarrays, not just find one. Ask if the array can be empty or if k can be negative.

2. Discuss brute force and its limitations

Mention that a brute force approach would check all O(n^2) subarrays, which is inefficient. Explain that negative numbers prevent using a sliding window technique.

3. Introduce prefix sum and hash map

Explain that a prefix sum is the sum of all elements from the start to the current index. Use a hash map to store the frequency of each prefix sum encountered so far.

4. Walk through the algorithm

Initialize a hash map with {0: 1} to handle subarrays starting at index 0. Iterate through the array, update the running sum, and for each element, add the frequency of (current_sum - k) to the count. Then increment the frequency of current_sum in the map.

5. Analyze complexity and edge cases

State that the time complexity is O(n) and space complexity is O(n). Discuss edge cases like empty array, all zeros, and large negative numbers.

Key Points to Mention

  • Prefix sum concept and how it helps find subarrays with sum k
  • Hash map to store prefix sum frequencies for O(1) lookups
  • Handling negative numbers: why sliding window fails
  • Initializing the hash map with {0: 1} to account for subarrays starting at index 0
  • Time and space complexity: O(n) time, O(n) space
  • Edge cases: empty array, k=0, all negative numbers

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.