Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
A sorted array holds the 40 values 2, 5, 8, … (step 3). Binary search with mid = ⌊(lo+hi)/2⌋ and 0-based indices looks for 59. How many elements are compared with 59 until it is found?
- A. 0
- B. 6
- C. 2
- D. 1
Correct answer: D. 1
Simulate: lo=0, hi=39; each probe halves the range; 59 sits at index 19 and is found on probe 1.
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
- Keys 17, 18, 96, 39, 87, 29 are inserted into a hash table of size 10 with h(k) = k mod 10 and linear probing. At which index is 29 stored?
- Quicksort uses the Lomuto partition with the last element as pivot on [7, 15, 50, 22, 27, 13, 16]. After one partition call, at which…
- Evaluate the postfix expression: 1 1 - 5 9 + 9 +
- How many swaps does bubble sort (adjacent swaps, ascending) make on [27, 39, 17, 5, 21, 29, 48, 12, 41]?
- A sorted array holds the 31 values 2, 5, 8, … (step 3). Binary search with mid = ⌊(lo+hi)/2⌋ and 0-based indices looks for 17. How many…
- Keys 88, 69, 39, 31, 97 are inserted into a hash table of size 8 with h(k) = k mod 8 and linear probing. At which index is 97 stored?