Question 31
MediumWhat is the output of this code?
n = 30
lo, hi = 0, n
while lo < hi:
mid = (lo + hi + ) //
mid * mid <= n:
lo = mid
:
hi = mid -
(lo)
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
n = 30
lo, hi = 0, n
while lo < hi:
mid = (lo + hi + ) //
mid * mid <= n:
lo = mid
:
hi = mid -
(lo)
4
25
5
6
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of searching a sorted n × m matrix (rows and columns sorted) starting from the top-right corner?
O(n + m)
O(log(n × m))
O(n × m)
O(n log m)
Sign up free to see the answer and a step-by-step explanation.
What is 'binary search on the answer'?
Binary searching over possible answer values, using a check of whether a value is feasible
A search that needs no sorted data
Searching for the word 'answer'
Searching two arrays at once
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of searching for a value in a balanced binary search tree with n nodes?
O(n)
O(1)
O(n log n)
O(log n)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
lo, hi, steps = 0, len(a) - 1, 0
while lo <= hi:
mid = (lo + hi) // 2
steps += 1
if a[mid] == 23:
break
if a[mid] < 23:
lo = mid + 1
else:
hi = mid - 1
print(mid, steps)
4 3
5 2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import bisect
a = [1, 2, 4, 4, 4, 7, 9]
print(bisect.bisect_left(a, 4), bisect.bisect_right(a, 4), bisect.bisect_left(a, 5))
3 5 5
2 5 6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [4, 5, 6, 7, 0, 1, 2]
lo, hi = 0, len(a) - 1
while lo < hi:
mid = (lo + hi) // 2
if a[mid] > a[hi]:
lo = mid + 1
else:
hi = mid
print(lo, a[lo])
6 2
0 4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def isqrt(n):
lo, hi = 0, n
while lo < hi:
mid = (lo + hi + 1) // 2
if mid * mid <= n:
lo = mid
else:
hi = mid - 1
return lo
print(isqrt(80), isqrt(81), isqrt(82))
8 9 9
9 9 9
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
piles, h = [3, 6, 7, 11], 8
lo, hi = 1, max(piles)
while lo < hi:
k = (lo + hi) // 2
if sum((p + k - 1) // k for p in piles) <= h:
hi = k
else:
lo = k + 1
print(lo)
5
6
Sign up free to see the answer and a step-by-step explanation.
How many comparisons does binary search need in the worst case for 1,000 sorted elements?
100
500
10
1,000
Sign up free to see the answer and a step-by-step explanation.
6 2
5 3
2 5 5
2 4 5
3 7
4 0
8 8 9
8 9 10
4
3