Question

A binary search function is returning -1 (not found) even when the target element is present in the array. The array is sorted. Which of the following is a common debugging pitfall related to the low and high pointers or loop condition?

A The mid calculation (low + high) / 2 causes an integer overflow for very large low and high values. Correct Answer Incorrect Answer
B The while loop condition is while (low < high) instead of while (low <= high). Correct Answer Incorrect Answer
C The low pointer is updated to mid instead of mid + 1. Correct Answer Incorrect Answer
D The high pointer is updated to mid instead of mid - 1. Correct Answer Incorrect Answer
E The array is not checked for emptiness before starting the search. Correct Answer Incorrect Answer

Solution

The correct answer is B

Practice Next
ask-question