Reported September 2026
Googlehash table

Find a Good-Subarray Endpoint Pair

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

Google flagged this one in September 2026, and the detail that matters is the tiebreak: you need the lexicographically smallest pair [i, j], minimizing i first, then j. With nums up to 200000 elements, the brute-force double loop is dead on arrival. It's a hash map problem dressed up as a pair search. If you've got an OA invite and 48 hours, learn this shape. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution as a backup.

The problem

Given an integer array nums and a nonnegative integer k, find two indices i < j such that |nums[i] - nums[j]| = k.
Return the lexicographically smallest valid pair [i, j]: minimize i, then j. Return [-1, -1] when no pair exists.

Function
findGoodPair(nums: int[], k: int) → int[]

Examples
Example 1
nums = [1,2,4,7]
k = 3
return = [0,2]
Case 1 exercises the documented deterministic contract.
Example 2
nums = [5,1,5]
k = 0
return = [0,2]
Case 2 exercises the documented deterministic contract.
Example 3
nums = [8,3,12,7]
k = 5
return = [0,1]
Case 3 exercises the documented deterministic contract.

Constraints
2 <= nums.length <= 200000.
-10^9 <= nums[i] <= 10^9.
0 <= k <= 2 * 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to flip the question. For each index i, you want the smallest j > i where nums[j] equals nums[i] + k or nums[i] - k. Scan right to left and keep a hash map from value to the smallest index seen so far (overwrite as you move left, so the map always holds the earliest index to the right of i). At each i, look up both targets, take the smaller j that exists, and record [i, j]. Because you're moving leftward, the last valid answer you record has the smallest i, so just keep overwriting. The pitfall is k = 0. Then both targets are the same value, and you must look up before inserting nums[i], or you'll pair an index with itself. Example 2 tests exactly this. Also watch the 2 * 10^9 bound on k: sums overflow 32-bit ints, so use 64-bit. If the live OA freezes you, StealthCoder is the hedge.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Find a Good-Subarray Endpoint Pair cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find a Good-Subarray Endpoint Pair FAQ

What's the trick to the Google good-pair problem?+

Use a hash map from value to earliest index, scanning right to left. For each i, check nums[i] + k and nums[i] - k in the map before inserting nums[i]. That gives O(n) time instead of the O(n^2) double loop, which won't pass at 200000 elements.

How do I get the lexicographically smallest pair?+

Minimize i first, then j. Scanning right to left, overwrite your answer every time you find a valid pair for the current i. The final overwrite has the smallest i. For that i, pick the smaller of the two candidate j indices from the map.

What breaks when k is 0?+

Both targets equal nums[i], so you could match an element with itself. Look up the map before inserting the current index. Then only indices to the right are present, and Example 2 ([5,1,5], k = 0) correctly returns [0,2].

Do I need to worry about integer overflow?+

Yes. Values reach 10^9 in magnitude and k reaches 2 * 10^9, so nums[i] + k can hit 3 * 10^9, past a signed 32-bit int. Use long in Java or C++. Python handles it automatically. Compare against targets, not differences, if you want to be careful.

How should I prepare for this in 48 hours?+

Write the right-to-left hash map solution from scratch twice, then test the k = 0 case, the no-pair case returning [-1,-1], and negative values. This is a two-sum variant, so the pattern should stick fast. Spend the rest of your time on similar hash map problems.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it