Nearest Value Replacement
Updated
Problem statement
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:
- Let
nextValue = A[cur] + 1. - Among all indices
iwith|i - cur| <= DandA[i] = nextValue, choose the nearest index. If distances tie, choose the smaller index. - If no such index exists, stop without changing
A[cur]. - Otherwise, set
A[cur] = nextValue, movecurto the chosen index, and continue.
Return the updated array.
Function
nearestValueReplacement(A: int[], cur: int, D: int) → int[] Complete nearestValueReplacement with the following parameters:
int[] A: the array to updateint cur: the zero-based starting indexint 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
Ais 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