Question 21
EasyHow fast can a peak element (one not smaller than its neighbours) be found in an array?
- 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
How fast can a peak element (one not smaller than its neighbours) be found in an array?
O(log n), using binary search
O(1)
O(n log n)
O(n²)
Sign up free to see the answer and a step-by-step explanation.
Which of these searches needs fast (O(1)) access to any position?
Searching a linked list from the head
Reading a tape from the start
Linear search
Binary search
Sign up free to see the answer and a step-by-step explanation.
Why is binary search a poor fit for a linked list?
Binary search needs strings
Reaching the middle node takes O(n) steps
Linked lists can't be sorted
Linked lists have no middle
Sign up free to see the answer and a step-by-step explanation.
In [1, 3, 5, 5, 8], what is the index of the first element that is ≥ 5 (the lower bound)?
2
4
1
3
Sign up free to see the answer and a step-by-step explanation.
In [1, 3, 5, 5, 8], what is the index of the first element that is > 5 (the upper bound)?
5
4
3
2
Sign up free to see the answer and a step-by-step explanation.
How can you count the occurrences of x in a sorted array in O(log n)?
Linear search from the start
Upper bound of x minus lower bound of x
Sort the array again
Use a stack
Sign up free to see the answer and a step-by-step explanation.
Binary search on the answer is used to find the integer part of √50. What is the result?
6
8
25
7
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import bisect
print(bisect.bisect_right([1, 3, 5, 5, 7], 5))
3
4
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(a[lo])
3
2
Sign up free to see the answer and a step-by-step explanation.
How do you find the first occurrence of a value in a sorted array with duplicates using binary search?
Stop at the first match
Use linear search only
When a match is found, record it and keep searching the left half
Search the right half on a match
Sign up free to see the answer and a step-by-step explanation.
5
2
4
0