← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
Jun 2026

Summary

Google SWE coding round, one algorithmic problem the whole session. The problem looked deceptively clean but the follow-up constraint is where things get interesting.

Questions Asked (1)

Q1

Given an integer array, find a pair of indices (i, j) where the values at both indices are equal and the subarray sum from i to j is maximized. Return the indices. Follow-up: solve it using O(1) extra space.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

My first instinct was a hashmap storing the last seen index of each value, then compute prefix sums between matching pairs.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: find indices i < j with arr[i] == arr[j] that maximize the sum of the subarray from i to j. Use prefix sums to compute subarray sums in O(1), and for each value, track the earliest occurrence to maximize the sum. For O(1) extra space, either sort the array with indices (O(n log n) time) or use a two-pass approach with constant extra variables.

Pro tip: Emphasize that the optimal pair for any value is always the first and last occurrence, as including more elements can only increase the sum if all numbers are non-negative; if negatives are allowed, this still holds because the sum from first to last includes all elements between, and any subarray between two equal values is contained within that range.

1. Clarify the problem

Confirm that i < j, values must be equal, and we want to maximize the sum of arr[i..j]. Ask if the array can contain negative numbers and if multiple pairs yield the same sum, which to return.

2. Brute force approach

Mention the O(n^2) solution: for each i, scan j > i, if arr[i] == arr[j], compute sum and track maximum. This establishes a baseline.

3. Optimize with prefix sums and hash map

Use a hash map to store the first occurrence of each value and prefix sums to compute subarray sums in O(1). Iterate through the array, and for each value, if seen before, compute sum from first occurrence to current index and update maximum.

4. Achieve O(1) extra space

For the follow-up, either sort the array while keeping original indices (O(n log n) time, O(1) extra space if in-place) and then scan for equal values, or use a two-pass approach with constant variables to track the best pair.

5. Analyze trade-offs

Compare time and space complexities: hash map gives O(n) time and O(n) space; sorting gives O(n log n) time and O(1) space. Discuss which is preferable based on constraints.

Key Points to Mention

  • Prefix sums allow O(1) subarray sum calculation.
  • Hash map stores first occurrence of each value to maximize subarray length.
  • The optimal pair for a value is its first and last occurrence.
  • For O(1) space, sorting with indices or two-pass scanning can be used.
  • Time-space trade-off: O(n) time with O(n) space vs O(n log n) time with O(1) space.
  • Edge cases: all elements distinct, negative numbers, multiple valid pairs.

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