Data Structures & AlgorithmsModerateProduct Company SDE · sorting OA

Problem: What is the minimum absolute difference between two values of the sorted list [1, 5, 6, 9]? Run the code.

Problem: What is the minimum absolute difference between two values of the sorted list [1, 5, 6, 9]? Run the code.
a = sorted([1, 5, 6, 9])
print(min(a[i + 1] - a[i] for i in range(len(a) - 1)))
What is printed?
  1. A. 2
  2. B. 1
  3. C. 3
  4. D. 4

Correct answer: B. 1

The adjacent pair 5 and 6 gives the smallest difference, 1. Comparing non-adjacent values after sorting is never needed.

More Coding Interview Problems questions

Exams that ask this topic