ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (finding the minimum in a rotated sorted array via binary search)? (Arrays are indexed from 0. All variables are integers, so / is integer division.)
What will be the output of the following pseudocode (finding the minimum in a rotated sorted array via binary search)? (Arrays are indexed from 0. All variables are integers, so / is integer division.)
Integer arr[7] = {4, 5, 6, 7, 0, 1, 2}
Integer lo = 0, hi = 6
while (lo < hi)
Integer mid = (lo + hi) / 2
if (arr[mid] > arr[hi])
lo = mid + 1
else
hi = mid
end if
end while
Print arr[lo]- A. 4
- B. 1
- C. 0
- D. 2
Correct answer: C. 0
This binary search exploits the rotation point: comparing the middle to the rightmost element tells which half is sorted, narrowing in on the true minimum, which is 0 (located at index 4).
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (moving all zeros to the end while preserving the relative order of non-zero elements…
- What will be the output of the following pseudocode (length of the longest substring without repeating characters, sliding window)?…
- What will be the output of the following pseudocode (number of bits needed to represent n in binary)? Function bitsNeeded(Integer n)…
- What will be the output of the following pseudocode (counting trailing zero bits in the binary representation of n)? (All variables are…
- What will be the output of the following pseudocode (recursively reversing a list)? Function reverseList(List lst) if (length(lst) <= 1)…
- What will be the output of the following pseudocode (counting set bits for every number from 0 to n using a DP recurrence)? (>> is right…