The Find the Distance Value Between Two Arrays interview question asks you to count how many elements in the first array (arr1) are "far enough" from every element in the second array (arr2). Specifically, an element contributes to the count if for every element , the absolute difference is strictly greater than a given value d.
Companies like Uber and Bloomberg use the Find the Distance Value Between Two Arrays coding problem to see if you can optimize a double-loop simulation. While the brute-force approach is , the sorted nature of differences allows for an solution using Binary Search. It evaluations your ability to handle inequalities efficiently.
This problem is best solved using Sorting and Binary Search.
arr2.arr1:arr2 using binary search (finding the insertion point).arr2 is greater than d.arr1 = [4, 5, 8], arr2 = [10, 9, 1, 8], d = 2
arr2: [1, 8, 9, 10].arr2 are 1 and 8. Differences: . Both . OK.arr2. Difference . . FAIL.
Result: 2.When checking if a value satisfies a condition against "all elements" of another array, always consider if sorting that second array allows you to only check the "worst case" (the closest element). This is a common Sorting interview pattern.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Pairs Whose Sum is Less than Target | Easy | Solve | |
| Two Sum Less Than K | Easy | Solve | |
| Number of Subsequences That Satisfy the Given Sum Condition | Medium | Solve | |
| The Latest Time to Catch a Bus | Medium | Solve | |
| Count the Number of Fair Pairs | Medium | Solve |