🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Binary Search Quiz: all 8 questions

Binary Search Quiz · Question 3 of 8

What does this return for nums = [1, 3, 5, 7], target = 4? (code)

What does this return for nums = [1, 3, 5, 7], target = 4?

lo, hi = 0, len(nums)
while lo < hi:
    mid = (lo + hi) // 2
    if nums[mid] < target:
        lo = mid + 1
    else:
        hi = mid
print(lo)
Answer & explanation

Correct answer: 2

This is lower bound: the first index with nums[i] ≥ 4 is index 2 (value 5) — also the insert position.

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 →