Data Structures & AlgorithmsHardCognizant · Capgemini pattern
while (lo <= hi) binary search runs on a sorted array of 31 elements for a key LARGER than every element. How many times does the loop body run?
- A. 31
- B. 5
- C. 6
- D. 4
Correct answer: B. 5
The range shrinks 31 → 15 → 7 → 3 → 1 → 0, one iteration per halving while the range is non-empty: 5 iterations (log₂ 32 = 5). 4 forgets the last one-element check; 6 counts the failing loop test as an iteration.
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
- A dynamic array starts with capacity 1 and doubles its capacity (copying all current elements) whenever an insert finds it full. How many…
- A stack-based checker is run on four strings. Which one is balanced?
- A circular queue in an array of size 6 uses front (index of the first item) and rear (index of the next free slot), both starting at 0…
- A 2-D array A[10][20] (indices from 0) of 4-byte integers is stored in COLUMN-major order starting at address 1000. What is the address of…
- Only the lower triangle (including the diagonal) of a 10 × 10 matrix is stored, row by row, in a 1-D array starting at address 2000, with…
- What is printed? (Python) python a = [3, 8, 1, 9, 4] a.insert(2, 7) a.pop() a.remove(9) print(a, len(a))