Data Structures & AlgorithmsModerateCognizant · Capgemini pattern
Binary search (one three-way comparison per probe) is done on a sorted array of 1000 elements. What is the maximum number of probes needed?
- A. 9
- B. 500
- C. 10
- D. 11
Correct answer: C. 10
The worst case is ⌊log₂ n⌋ + 1 = ⌊9.97⌋ + 1 = 10 probes, because 2¹⁰ − 1 = 1023 ≥ 1000 while 2⁹ − 1 = 511 is too small. Taking ⌊log₂ 1000⌋ = 9 forgets the final probe.
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
- This code tries to reverse the singly linked list 1 → 2 → 3 → 4, but its last line has been changed from cur = nxt. What does the list…
- A linked list has 3 nodes before the loop starts and a loop of 5 nodes. Floyd's slow pointer moves 1 step and fast pointer 2 steps per…
- ^ is exponentiation (highest precedence, right-associative). What is the postfix form of A + B C ^ D ^ E − F?
- What is the value of the postfix expression 2 3 2 ^ ^ (^ means power)?
- What is the minimum stack size needed to evaluate the postfix expression a b c d + e f − / + ?
- A queue is built from two stacks: enqueue pushes onto stack IN; dequeue pops from stack OUT, first moving everything from IN to OUT only…