Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Binary Search Iterative Approach in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Binary Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Binary Search for Existing Element
What is the output of the following code when searching for the number 7 in the list?
DSA Python
def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

arr = [1, 3, 5, 7, 9, 11]
print(binary_search(arr, 7))
A4
B2
C-1
D3
Attempts:
2 left
💡 Hint
Remember that list indices start at 0 and the list is sorted.
❓ Predict Output
intermediate
2:00remaining
Output When Element is Not Present
What will the code print when searching for the number 7 in the list?
DSA Python
def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

arr = [2, 4, 6, 8, 10]
print(binary_search(arr, 7))
A0
B-1
C2
D3
Attempts:
2 left
💡 Hint
If the element is not found, the function returns -1.
🧠 Conceptual
advanced
2:00remaining
Why Use Iterative Binary Search Instead of Recursive?
Which of the following is the best reason to prefer iterative binary search over recursive binary search?
ARecursive binary search cannot find elements in sorted lists.
BIterative binary search is easier to write and understand than recursive binary search.
CIterative binary search uses less memory because it avoids the overhead of recursive calls.
DIterative binary search always runs faster than recursive binary search.
Attempts:
2 left
💡 Hint
Think about what happens with function calls in recursion.
❓ Predict Output
advanced
2:00remaining
Output of Binary Search with Duplicate Elements
What index will the binary search return when searching for 5 in the list with duplicates?
DSA Python
def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

arr = [1, 3, 5, 5, 5, 7, 9]
print(binary_search(arr, 5))
A3
B4
C2
D-1
Attempts:
2 left
💡 Hint
Binary search may return any index where the target is found.
🔧 Debug
expert
2:00remaining
Identify the Bug in Binary Search Implementation
What error will this code produce when searching for 15 in the list?
DSA Python
def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left < right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

arr = [5, 7, 10, 15]
print(binary_search(arr, 15))
A-1
B2
CIndexError
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition carefully and think about when the loop stops.

Practice

(1/5)
1. What is the main purpose of the mid variable in the iterative binary search algorithm?
easy
A. To store the target value we are searching for
B. To keep track of the number of iterations
C. To find the middle index of the current search range
D. To hold the entire list being searched

Solution

  1. Step 1: Understand the role of mid in binary search

    The mid variable calculates the middle index between the current low and high indices to split the search range.
  2. Step 2: Explain why mid is important

    By checking the element at mid, the algorithm decides whether to search the left or right half next.
  3. Final Answer:

    To find the middle index of the current search range -> Option C
  4. Quick Check:

    mid = middle index [OK]
Hint: Remember binary search splits list by middle index [OK]
Common Mistakes:
  • Confusing mid with the target value
  • Using mid as a counter instead of index
  • Not updating mid inside the loop
2. Which of the following is the correct way to calculate the middle index mid in an iterative binary search in Python?
easy
A. mid = (low + high) // 2
B. mid = low - high // 2
C. mid = low + high
D. mid = (low + high) / 2

Solution

  1. Step 1: Identify correct integer division in Python

    In Python, // performs integer division, which is needed for an index.
  2. Step 2: Explain why / is incorrect

    / returns a float, which cannot be used as a list index.
  3. Final Answer:

    mid = (low + high) // 2 -> Option A
  4. Quick Check:

    Use integer division for index [OK]
Hint: Use // for integer division in Python [OK]
Common Mistakes:
  • Using / which returns float
  • Adding low and high without division
  • Incorrect operator precedence without parentheses
3. What will be the output of the following code snippet?
def binary_search(arr, target):
    low, high = 0, len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

arr = [2, 4, 6, 8, 10]
print(binary_search(arr, 6))
medium
A. 1
B. 3
C. -1
D. 2

Solution

  1. Step 1: Trace the binary search for target 6

    Initial low=0, high=4, mid=2. arr[2]=6 matches target.
  2. Step 2: Return the index where target is found

    Since arr[mid] == target, function returns 2 immediately.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Index of 6 in array is 2 [OK]
Hint: Check mid index value first to find target fast [OK]
Common Mistakes:
  • Confusing index with value
  • Returning value instead of index
  • Off-by-one errors in mid calculation
4. Identify the error in the following iterative binary search code:
def binary_search(arr, target):
    low = 0
    high = len(arr)
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1
medium
A. high should be initialized as len(arr) - 1
B. low should start from 1 instead of 0
C. mid calculation should use float division
D. The loop condition should be while low < high

Solution

  1. Step 1: Check initialization of high

    High is set to len(arr), which is out of valid index range (0 to len(arr)-1).
  2. Step 2: Explain correct initialization

    High should be len(arr) - 1 to avoid index out of range errors when accessing arr[mid].
  3. Final Answer:

    high should be initialized as len(arr) - 1 -> Option A
  4. Quick Check:

    High = len(arr) - 1 for valid index [OK]
Hint: Array indices go from 0 to length-1 [OK]
Common Mistakes:
  • Setting high to len(arr) causes index error
  • Changing low start incorrectly
  • Using float division for mid
5. Given a sorted list arr = [1, 3, 5, 7, 9, 11, 13], how many iterations will the iterative binary search take to find the target 7?
medium
A. 3
B. 1
C. 2
D. 4

Solution

  1. Step 1: Trace iterations to find 7

    Iteration 1: low=0, high=6, mid=3, arr[3]=7 matches target.
  2. Step 2: Count iterations

    Since target found at first mid check, total iterations = 1.
  3. Step 3: Reconsider counting loop passes

    Loop runs once, but mid calculation and comparison count as one iteration.
  4. Final Answer:

    1 -> Option B
  5. Quick Check:

    Target found at first mid check [OK]
Hint: Count each mid calculation as one iteration [OK]
Common Mistakes:
  • Counting iterations incorrectly
  • Assuming always 3 iterations for 7 elements
  • Confusing iterations with index value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard