Data Structures & AlgorithmsModerateTCS NQT · binary search
Problem: What is the number of comparisons needed in the worst case to check whether a sorted array of n elements contains a given value by binary search?
- A. n
- B. n / 2
- C. 1
- D. ceil(log2(n + 1))
Correct answer: D. ceil(log2(n + 1))
Each comparison halves the remaining interval, so about log2(n) + 1 comparisons suffice.
More Coding Interview Problems questions
- Problem: What is the greedy choice used in the 'minimum number of arrows to burst balloons' problem?
- Problem: Which problem is solved by the Gale-Shapley algorithm in O(n^2) time with preference lists?
- Problem: Which data structure supports adding a value to a prefix of an array and finding the first index whose value is at least x, each…
- Problem: What is the time complexity of computing the diameter of a tree with n vertices by two breadth-first searches?
- Problem: How many integers in [1, 1000] are divisible by both 4 and 9? Run the code. python print(sum(1 for x in range(1, 1001) if x % 4…
- Problem: What is the sum of the decimal digits of 2^15? Run the code. python print(sum(int(c) for c in str(2 15))) What is printed?