FastPrepNearest Value Replacement
Problems/Nearest Value Replacement

Nearest Value Replacement

Google logoGoogle● MediumFULLTIMEPHONE SCREEN

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The source says to update A[cur] before searching. In the example, that would change A[1] from 3 to 4 on the second step, producing [1, 4, 3, 3, 4, 5, 2] instead of the shown [1, 3, 3, 3, 4, 5, 2]. Because the shown output keeps A[1] as 3, this version searches for A[cur] + 1 first and only updates A[cur] when a valid next index is found. The judged core task matches the visible source at about 97%.

You are given an integer array A of length N, a zero-based starting index cur, and a distance D.

Update the array by repeating these steps:

  1. Let nextValue = A[cur] + 1.
  2. Among all indices i with |i - cur| <= D and A[i] = nextValue, choose the nearest index. If distances tie, choose the smaller index.
  3. If no such index exists, stop without changing A[cur].
  4. Otherwise, set A[cur] = nextValue, move cur to the chosen index, and continue.

Return the updated array.

Function

nearestValueReplacement(A: int[], cur: int, D: int) → int[]

Complete nearestValueReplacement with the following parameters:

  1. int[] A: the array to update
  2. int cur: the zero-based starting index
  3. int D: the maximum search distance

Examples

Example 1

A = [1, 3, 2, 3, 4, 5, 2]cur = 2D = 2return = [1, 3, 3, 3, 4, 5, 2]

At index 2, the next value is 3. Indices 1 and 3 both contain 3 at distance 1, so choose index 1 and set A[2] to 3. From index 1, the next value is 4, but the only 4 is at index 4, which is outside distance 2. Stop without changing A[1].

Constraints

  • A is non-empty.
  • 0 <= cur < A.length.
  • D >= 0.

More Google Phone Screen questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
  • Determine the Winner of Three-Card HandsOpenAI · PHONE SCREENOpenAI interview questions
  • Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
  • Asteroid CollisionSiemens · PHONE SCREEN
  • Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
  • Order and Batch Open-Close LifecycleHudson River Trading · PHONE SCREENHudson River Trading interview questions
  • Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
  • Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
  • Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions
  • Realized Gains from a Real-Estate Transaction StreamKikoff · PHONE SCREENKikoff interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] nearestValueReplacement(int[] A, int cur, int D) {
      // write your code here
    }

}
Checking account…