Data Structures & AlgorithmsHardProduct Company SDE · binary search round
Problem: What is the worst-case time complexity of binary search for the first occurrence of a value in a sorted array that contains duplicates?
- A. O(n)
- B. O(log n)
- C. O(n log n)
- D. O(sqrt n)
Correct answer: B. O(log n)
A modified binary search that keeps searching left after a match finds the first occurrence in O(log n) comparisons.
More Coding Interview Problems questions
- Problem: Which algorithm constructs an Euler circuit of a connected graph whose vertices all have even degree in linear time?
- Problem: What is the time complexity of computing the Z-array of a string of length n?
- Problem: Which deterministic algorithm finds the global minimum cut of an undirected weighted graph with V vertices in O(V^3) time?
- Count the non-empty subarrays of a = [3, -1, 4, -1, 5, -9, 2, 6] whose sum is divisible by 5, using prefix sums taken modulo 5. python…
- Items (weight, value) are (5,10), (4,7), (6,12), (3,4) and the knapsack capacity is 10. Each item may be taken at most once. Run the exact…
- Binary string s = "1101001110001011". Find the length of the longest contiguous substring that contains equally many 0s and 1s. python…