Find K Closest Elements in a Sorted Array
Updated
Problem statement
Given a sorted integer array arr, an integer k, and a target x, return the k closest values to x in ascending order.
A value a is closer than b when |a - x| < |b - x|. For equal distances, the smaller value is closer.
Function
findClosestElements(arr: int[], k: int, x: int) → List<Integer>Examples
Example 1
arr = [1,2,3,4,5]k = 4x = 3return = [1,2,3,4]The four smallest distances belong to 1, 2, 3, and 4.
Example 2
arr = [1,2,3,4,5]k = 4x = -1return = [1,2,3,4]The target lies left of the array, so the first four values are closest.
Constraints
arris non-empty and sorted in nondecreasing order.1 <= k <= arr.length.- All values and
xare signed integers.
More Bloomberg LP Onsite Interview questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Count Winning Dice Face PairsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- K Smallest Squares from a Sorted ArrayUber · ONSITE INTERVIEWUber interview questions
- Find K Closest Elements in a Sorted ArrayLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Find K Closest Elements in a Sorted ArrayNavi · PHONE SCREEN
- K Closest Elements in a Sorted ArrayAmazon · PHONE SCREEN
- Count Centaur Friend RequestsMeta · PHONE SCREENMeta interview questions
- Count the Number of Incremovable Subarrays IIThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions
- Count Triples That Can Form a TriangleAdobe · PHONE SCREENAdobe interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)