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?
- A. 2
- B. 1
- C. 3
- 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
- Problem: How many 2-element subsets of {1, 2, 3, 4, 5} have an even sum? Run the code. python from itertools import combinations…
- Problem: Which statement about the sparse table data structure for static range-minimum queries is correct?
- Problem: Which statement about heavy-light decomposition for path queries on a tree is correct?
- Problem: Which statement about Fermat's little theorem is correct?
- Problem: What is the time for one point update and one rectangle-sum query in a 2D Fenwick tree on an n x n grid?
- Problem: Why can subtree queries on a rooted tree be answered with a range query on an array?