Data Structures & AlgorithmsHardGoogle · Flipkart interview
Search for a value in a sorted array that was rotated and MAY CONTAIN DUPLICATES (e.g. [1, 1, 1, 1, 0, 1, 1]). What is the worst-case time of the best comparison-based algorithm?
- A. O(log n), the same as without duplicates
- B. O(log² n), by binary searching for the pivot first
- C. O(n), because when a[lo] = a[mid] = a[hi] the half holding the target cannot be identified
- D. O(√n), by probing blocks of size √n
Correct answer: C. O(n), because when a[lo] = a[mid] = a[hi] the half holding the target cannot be identified
With all equal values except one hidden element, every probe reveals nothing about where the odd one is, so any algorithm must inspect Ω(n) positions in the worst case. The usual fix (lo += 1, hi −= 1 when the ends equal mid) is therefore linear in the worst case.
More Coding Interview Problems questions
- Problem: Return the index of ANY peak element (strictly greater than its neighbours; a[−1] = a[n] = −∞) in O(log n). python def peak(a)…
- Problem: In an n × n matrix whose rows and columns are both sorted, find the k-th smallest value by binary searching on the VALUE and…
- Problem: Reverse the nodes of a singly linked list from position m to n (1-based) in one pass. python class N: def init(s, v, nx=None)…
- Problem: Group a linked list's nodes at odd POSITIONS first, then the nodes at even positions, keeping relative order. python class N: def…
- Deep-copy a linked list whose nodes also have a random pointer. Which approach uses O(n) time and only O(1) extra space (besides the…
- Problem: Simulate an LRU cache of capacity 3 (get returns −1 on a miss; a get or put marks the key most recently used). python from…