Finding why a shrinking search stops progressing

An integer-search routine should locate target within the inclusive interval from low to high. The initial target is guaranteed to lie in that interval. A student reports that the following Python loop never finishes:


Python
target = 11
low = 0
high = 16
iteration = 0

while low < high:
    iteration += 1
    mid = (low + high) // 2
    if mid < target:
        low = mid
    else:
        high = mid


For the nonnegative integers used here, // discards any fractional part. Count an iteration whenever the loop body executes.


Find the first iteration on which neither bound changes, and give low and high immediately after that iteration. Then the assignment to low is repaired as low = mid + k. Determine the largest positive integer k for which low = mid + k guarantees progress and retains every possible integer target for any valid initial interval under the stated assumptions. Keep the other statements unchanged.


Enter all four answers as exact integers: the iteration count, the two bound values, and the increment k.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Software Debugging Questions: