Reported March 2024
Googlebinary search

Nearest Value Replacement

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's March 2024 OA included a problem called Nearest Value Replacement. You're looking at a problem that sounds simple but trips candidates on implementation. No verbatim text is available yet, but the title tells you the core idea: you need to find the nearest value to some target and swap or replace it. This is a search-and-replace pattern that lives in the hash-table and sorting space. StealthCoder can spot the efficiency trap in real time if you freeze on the optimal approach.

Pattern and pitfall

The trick here is understanding what 'nearest' means in context. You're probably iterating through values, computing distances, and tracking the minimum. The naive approach is O(n) per query, but if you're doing multiple replacements, you'll TLE. The smart move is to sort the input first, then binary-search or two-pointer the nearest value. Common pitfall: candidates over-complicate the distance metric or forget to handle ties (closest value appears twice, which one do you pick). If you blank on the binary-search edge case during the live OA, StealthCoder will outline the pattern in seconds.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Nearest Value Replacement 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Nearest Value Replacement FAQ

What does 'nearest value' actually mean here?+

It means the value in the array with the smallest absolute difference from your target. If two values are equidistant, you need to check if the problem specifies a tiebreaker (often it's the smaller value, or the one that appears first).

Is this a binary search problem?+

Very likely. Sort the array, then binary-search for the target. Check the neighbors of the insertion point to find the closest value. That's your O(log n) lookup per query.

What's the gotcha Google tests here?+

Handling edge cases: what if the target is smaller than all values, or larger? What if there's an exact match? Write out your tie-breaking rule before you code.

Can I solve this in 20 minutes?+

Yes, if you recognize it as a binary-search problem. Sort, code the binary-search, check left and right neighbors, return the closer one. The implementation is short if you see the pattern.

Is this still asked at Google?+

Confirmed in March 2024. It's a medium-difficulty problem that tests whether you know when to sort and binary-search instead of brute force. Expect it or similar patterns.

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