Question 51
MediumWhich data structure lets you search, insert and delete in O(log n) and also keeps elements sorted?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
Which data structure lets you search, insert and delete in O(log n) and also keeps elements sorted?
A queue
A hash table
A balanced binary search tree
An unsorted array
Sign up free to see the answer and a step-by-step explanation.
You search for 'the first bad version' among versions 1 to n, where all versions after a bad one are bad. Which approach is best?
Sort the versions
Check versions in random order
Check every version
Binary search on the version number
Sign up free to see the answer and a step-by-step explanation.
What does binary search return if the target is not present and the loop ends with lo > hi?
Always −1
The target's index
The middle index
lo is the index where the target would be inserted
Sign up free to see the answer and a step-by-step explanation.
What is the worst-case time of searching an unbalanced BST built by inserting sorted values?
O(log n)
O(n log n)
O(n)
O(1)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a, t = [4, 5, 6, 7, 0, 1, 2], 0
lo, hi = 0, len(a) - 1
ans = -1
while lo <= hi:
mid = (lo + hi) // 2
if a[mid] == t:
ans = mid
break
if a[lo] <= a[mid]:
if a[lo] <= t < a[mid]:
hi = mid - 1
else:
lo = mid + 1
else:
if a[mid] < t <= a[hi]:
lo = mid + 1
else:
hi = mid - 1
print(ans)
4
6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 2, 3, 1]
lo, hi = 0, len(a) - 1
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < a[mid + 1]:
lo = mid + 1
else:
hi = mid
print(lo)
1
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
w, days = list(range(1, 11)), 5
def ok(cap):
d, load = 1, 0
for x in w:
if load + x > cap:
d += 1
load = 0
load += x
return d <= days
lo, hi = max(w), sum(w)
while lo < hi:
mid = (lo + hi) // 2
if ok(mid):
hi = mid
else:
lo = mid + 1
print(lo)
15
20
Sign up free to see the answer and a step-by-step explanation.
What is the average time complexity of interpolation search on uniformly distributed sorted data?
O(log n)
O(n)
O(log log n)
O(1)
Sign up free to see the answer and a step-by-step explanation.
When is exponential search (doubling the range, then binary search) useful?
When all elements are equal
When searching an unbounded or very large sorted list where the target is near the start
Only for linked lists
When the array is unsorted
Sign up free to see the answer and a step-by-step explanation.
How can you find the median of two sorted arrays of sizes m and n in O(log(min(m, n))) time?
Merge both arrays
Use a hash map
Binary search for a partition of the smaller array that balances the left and right halves
Sort the combined array
Sign up free to see the answer and a step-by-step explanation.
2
-1
0
3
18
13