Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: Binary search for the value 77 in the sorted list 1, 2, ..., 100 uses the midpoint mid = (lo + hi) // 2 and the inclusive range [lo, hi] starting from [0, 99]. How many comparisons (midpoints examined) are needed?
- A. 5
- B. 7
- C. 6
- D. 77
Correct answer: B. 7
Midpoints 49, 74, 87, 80, 77, 75, 76 (indices) are examined, finding value 77 at index 76 on the seventh. Searching for index 77 gives 5, not counting the final hit gives 6, and a linear scan needs 77.
More Coding Interview Problems questions
- Problem: What is the value of the lowest set bit of 96 (the largest power of 2 dividing 96)?
- Problem: For the numbers 1, 2, 3, 4, 5, how many unordered pairs (i, j) with i < j have i XOR j strictly less than 4?
- Problem: What is the sum of the proper divisors of 220 (divisors less than 220)?
- Problem: What is the smallest positive integer n such that 1 + 2 + … + n is divisible by 10?
- Problem: Compute 7^100 mod 13.
- Problem: Using US coin denominations 1, 5, 10, 25, what is the minimum number of coins needed to make 63 (greedy works for these…