Find the Closest Element to a Target in an Array
Given an integer array and a target value, find the element in the array that is closest to the target (minimum absolute difference). If two elements are equally close to the target, return the larger of the two. Example: arr = [3, 11, 13, 23, 65, 22], target = 12 → output 13 (both 11 and 13 are distance 1 away, but 13 is larger).
Asked at:
Walmart
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Mid July, 2026
Walmart
Senior
Given an integer array and a target value, find the array element that is closest to the target (minimum absolute difference). If two elements are equally close, return the larger element. Input arr[]: an array of integers target: an integer Output A single integer: the closest element in arr to target Tie-break rule: if distances are equal, return the greater value Example Input: arr = [3, 11, 13, 23, 65, 22], target = 12 Distances: |11-12| = 1, |13-12| = 1 (tie) Output: 13 (larger among tied closest elements)
Hello Interview Premium
Your account is free and you can post anonymously if you choose.