← Binary Search Quiz: all 8 questions
Binary Search Quiz · Question 8 of 8
Which loop pairing can cause an infinite loop?
Which loop pairing can cause an infinite loop?
- A.while lo <= hi with lo = mid + 1 / hi = mid − 1
- B.while lo < hi with lo = mid + 1 / hi = mid
- C.while lo < hi with lo = mid / hi = mid − 1 (and mid rounded down)
- D.None of these
Answer & explanation
Correct answer: while lo < hi with lo = mid / hi = mid − 1 (and mid rounded down)
With mid rounded down and lo = mid, when hi = lo + 1 the value mid = lo, so lo never changes.
Can you get the rest right without peeking?
Take the timed Binary Search Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →