Question 41
MediumWhy is mid = lo + (hi − lo) / 2 preferred over (lo + hi) / 2 in Java or C?
- 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
Why is mid = lo + (hi − lo) / 2 preferred over (lo + hi) / 2 in Java or C?
It works on unsorted arrays
lo + hi can overflow for large indexes
It gives a different answer
It is faster
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of searching a row-wise and column-wise sorted n × n matrix by starting at the top-right corner?
O(n)
O(log n)
O(n²)
O(n log n)
Sign up free to see the answer and a step-by-step explanation.
Which search needs the data to be sorted?
Linear search
Binary search
Breadth-first search
Hash lookup
Sign up free to see the answer and a step-by-step explanation.
A sorted array has been rotated at an unknown point. What is the time complexity of searching it with a modified binary search (distinct values)?
O(n)
O(1)
O(n log n)
O(log n)
Sign up free to see the answer and a step-by-step explanation.
How can you find the peak element (greater than its neighbours) of an array in O(log n)?
Binary search moving towards the larger neighbour of mid
Sort the array
Use a stack
Linear scan only
Sign up free to see the answer and a step-by-step explanation.
Ternary search is used to find what?
Duplicates
Shortest paths
Any value in an unsorted array
The maximum or minimum of a unimodal function
Sign up free to see the answer and a step-by-step explanation.
When does linear search beat binary search in practice?
For very small or unsorted arrays, where sorting first would cost more
For very large sorted arrays
When values are integers
Never
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of finding how many times x occurs in a sorted array?
O(log n), using two binary searches for the first and last positions
O(1)
O(n log n)
O(n) always
Sign up free to see the answer and a step-by-step explanation.
In the 'allocate books' (minimise the maximum pages) problem, what is binary searched?
The number of students
The sorted order of books
The maximum number of pages a student may get
The index of the book
Sign up free to see the answer and a step-by-step explanation.
Jump search on a sorted array of n elements uses which block size for best performance?
log n
√n
1
n/2
Sign up free to see the answer and a step-by-step explanation.