Data Structures & AlgorithmsModerateTCS NQT · binary search warm-up
Problem: What is floor(sqrt(2000)) found by binary search? Run the code.
Problem: What is floor(sqrt(2000)) found by binary search? Run the code.
lo, hi = 0, 2000
while lo < hi:
mid = (lo + hi + 1) // 2
if mid * mid <= 2000:
lo = mid
else:
hi = mid - 1
print(lo)What is printed?- A. 44
- B. 45
- C. 43
- D. 2000
Correct answer: A. 44
44^2 = 1936 <= 2000 and 45^2 = 2025 > 2000, so the search converges to 44. Using the upper mid (lo + hi) // 2 without +1 can loop forever on this template.
More Coding Interview Problems questions
- Problem: Tasks with (duration, deadline) are (3,5), (2,4), (4,9), (1,3). Scheduling by earliest deadline first, how many tasks finish on…
- Problem: Which statement about the greedy algorithm for 'minimum number of lecture rooms' (interval partitioning) is correct?
- Problem: Which statement about bitmask DP for a Hamiltonian path problem on n = 20 vertices is correct in the standard sense?
- Problem: Which claim about a binary search over a rotated sorted array (no duplicates) finding the minimum element is correct?
- Problem: How many pairs (i < j) of a = [1, 2, 4, 5, 7, 8] have a[i] - a[j] divisible by 3? Run the code. python a = [1, 2, 4, 5, 7, 8]…
- Problem: What is the minimum number of meeting rooms needed for intervals [0,30], [5,10], [15,20]? Run the sweep-line code. python iv =…