Data Structures & AlgorithmsModerateInfosys · Cognizant pattern
Binary search with mid = (lo + hi) / 2 (integer division) looks for 31 in [3, 9, 14, 20, 27, 31, 40] (0-based). Which indices are probed, in order?
- A. 3, 4, 5
- B. 3, 5, 6
- C. 3, 5
- D. 4, 5
Correct answer: C. 3, 5
lo = 0, hi = 6 → mid 3 (20 < 31) → lo = 4. mid = (4 + 6)/2 = 5 → a[5] = 31, found. Two probes: 3, 5. Probing 4 next would be linear scanning, not halving.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Starting from an empty stack: push 5, push 3, pop, push 8, push 2, pop, pop, push 7, push 1. What are the stack contents from top to bottom?
- A circular queue uses an array of size 5 (indices 0–4); front is the index of the first element and rear is the index of the last element…
- Double hashing in a table of size 11 uses h1(k) = k mod 11 and h2(k) = 7 − (k mod 7); probe i goes to (h1 + i·h2) mod 11. Keys 14, 25, 36…
- How many DISTINCT palindromic substrings (counted as strings, not by position) does 'abaab' have?
- For each element of [4, 10, 5, 8, 20, 15, 3, 12], find the nearest SMALLER element to its LEFT (−1 if none) using a monotonic stack. What…
- The numbers 1, 2, 3, 4, 5 are pushed onto a stack in that order, and pops may happen at any time. Which of these pop sequences is…