Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: A sorted array [1, 3, 3, 5, 8, 8, 8, 13, 21] is searched for the value 8 using the standard lower-bound binary search (find the first index whose value is ≥ 8, with the search range [lo, hi) starting at [0, 9)). Which index does the search return?
- A. 4
- B. 5
- C. 3
- D. 6
Correct answer: A. 4
Lower-bound search returns the first position with value ≥ x, so duplicates of 8 are skipped to the leftmost. Upper-bound search gives the index after the last 8 instead.
More Coding Interview Problems questions
- Problem: A tree has 7 nodes 1–7 with edges 1-2, 1-3, 2-4, 2-5, 3-6, 6-7. Each node is coloured red if its depth from node 1 is even and…
- Problem: Starting with the sorted list 1, 4, 9, 16, 25, 36, two operations alternate: first remove the largest element, then append the…
- Problem: A simple graph has 6 vertices labelled 1–6. Vertex i is adjacent to j exactly when gcd(i, j) = 1 and i ≠ j. How many edges does…
- Problem: What is the number of inversions in the array [6, 2, 8, 1, 9, 4, 3, 7, 5]? (An inversion is a pair of indices i < j with a[i] >…
- Problem: Using a single counter, a sequence of commands is applied to the value x = 0: "+5", "×2", "−3", "×2", "+1", "÷4" (integer…
- Problem: In a binary max-heap stored in an array with 1-based indexing, the array is [40, 30, 35, 10, 20, 25, 28, 5, 8, 12]. An operation…