← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round, one question on subarray sums. Pretty standard algorithmic problem but the O(n) solution requires you to actually know the prefix-sum trick, which I'd practiced but still fumbled explaining cleanly under pressure.

Questions Asked (1)

Q1

Given an integer array and an integer k, find the total number of contiguous subarrays whose elements sum to k. Walk through an efficient solution.

Algorithms & Data Structures
Author's notes

I knew this one.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (e.g., array size, element ranges, whether negative numbers are allowed) and then present a brute-force O(n^2) approach. Follow with an optimized O(n) solution using a hash map to store prefix sums and their frequencies, explaining how it avoids redundant computations. Walk through a concrete example to illustrate the logic and discuss edge cases.

Pro tip: Mention that the hash map approach works even with negative numbers, unlike sliding window, and emphasize that you're optimizing for time complexity while using O(n) extra space. This shows you understand trade-offs and can adapt to constraints.

1. Clarify the problem

Ask about input constraints (array size, element range, negative numbers) and confirm that subarrays must be contiguous. This ensures you don't make incorrect assumptions.

2. Discuss brute-force approach

Briefly explain the O(n^2) solution: iterate over all possible subarrays and check if their sum equals k. This establishes a baseline and shows you can think of a simple solution.

3. Introduce optimized approach

Explain that we can use a hash map to store the frequency of prefix sums. For each element, compute the running sum and check if (running sum - k) exists in the map; if so, add its frequency to the count.

4. Walk through an example

Choose a small array (e.g., [1,2,3], k=3) and step through the algorithm, showing how the hash map is updated and how the count is incremented.

5. Analyze complexity and edge cases

State that time complexity is O(n) and space is O(n). Discuss edge cases: empty array, k=0, negative numbers, and large arrays.

Key Points to Mention

  • Prefix sum concept: sum of subarray from i to j = prefix[j] - prefix[i-1].
  • Hash map stores prefix sum frequencies, initialized with {0:1} to handle subarrays starting at index 0.
  • Time complexity O(n) and space complexity O(n).
  • Works with negative numbers and zeros, unlike sliding window.
  • Edge cases: empty array, k=0, all elements zero, large input.
  • Alternative approaches: brute-force O(n^2) and sliding window (only for non-negative numbers).

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