Data Structures & AlgorithmsModerateTCS NQT · binary search
Problem: What is the expected number of comparisons for a binary search on a sorted array of n = 1000 elements (successful search, worst case)?
- A. 1000 comparisons
- B. 500 comparisons
- C. ceil(log2(1001)) = 10 comparisons in the worst case
- D. log2(1000) rounded down to 9
Correct answer: C. ceil(log2(1001)) = 10 comparisons in the worst case
The worst case is about log2(n) + 1 comparisons, which is 10 for n = 1000.
More Coding Interview Problems questions
- Problem: What is the time complexity of finding the articulation points and bridges together with one DFS?
- Problem: How many pairs (i < j) of integers in [1, 30] have i + j divisible by 6? Run the code. python print(sum(1 for i in range(1, 31)…
- Problem: How many integers in [1, 10000] are divisible by 9 but not by 27? Run the code. python print(sum(1 for x in range(1, 10001) if x…
- Problem: How many trailing zero bits (factors of 2) does 1000! have? Run the code. python n = 1000 c = 0 p = 2 while p <= n: c += n // p p…
- Problem: How many distinct values does (i j) mod 7 take for 1 <= i, j <= 7? Run the code. python print(len({(i j) % 7 for i in range(1, 8)…
- Problem: What is the maximum sum of non-adjacent elements of the circular array [3, 2, 7, 10]? Run the code. python def lin(a): inc = exc…