Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: A sorted array [2, 3, 5, 7, 11, 13, 17, 19] is searched for 11 using binary search with mid = (lo + hi) // 2 over the inclusive range [0, 7]. What is the index where 11 is found?
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
The midpoint of [0, 7] is 3 (value 7), so the search moves right; the next midpoint of [4, 7] is 5 (value 13), so it moves left and finds 11 at index 4.
More Coding Interview Problems questions
- Problem: Consider the string "xyzzyzyx". How many distinct substrings of length 2 are there?
- Problem: The array [1, 4, 6, 3, 8, 2] is processed by a single pass that tracks the maximum so far. How many elements are records…
- Problem: A set of 10 people is divided into 5 pairs. How many distinct ways are there to form these pairs (unordered pairs, unordered…
- Problem: In the array [5, 8, 3, 7, 2], find the second-largest element.
- Problem: A tree with 5 nodes is given by the parent array p = [−1, 0, 0, 1, 1] (node 0 is the root). How many leaves does it have?
- Problem: The bit pattern 10110 is shifted left by 2 positions and then XOR-ed with the original pattern. What is the decimal value of the…