Data Structures & AlgorithmsHardInfosys SP/DSE · Product Company SDE
Problem: A problem is solved via binary search on the answer (not on an array index) -- e.g. 'minimum capacity to ship packages within D days'. What property must the problem have for this technique to apply?
- A. The input array must already be sorted
- B. The answer must always be an array index
- C. The feasibility check (can we achieve X?) must be monotonic in X: if X works, every larger (or smaller, depending on direction) value also works
- D. There must be no feasible answer at all
Correct answer: C. The feasibility check (can we achieve X?) must be monotonic in X: if X works, every larger (or smaller, depending on direction) value also works
Binary search on the answer relies on a monotonic predicate: once a candidate value is feasible, every value past it in the same direction stays feasible (or infeasible), letting binary search narrow in on the boundary.
More Coding Interview Problems questions
- Problem: Which of these is the correct worst-case time complexity of building a max-heap from an array of n unsorted elements using the…
- Problem: In a graph represented with an adjacency list, what is the space complexity, and how does it compare to an adjacency matrix for a…
- Problem: Which best explains why the greedy 'interval scheduling to maximize the number of non-overlapping intervals' problem is solved…
- Problem: Which is the correct worst-case time complexity for inserting into a balanced BST (like a Red-Black or AVL tree) with n nodes…
- Problem: What is the time complexity of the standard recursive merge sort, including the merge step, expressed via its recurrence T(n) =…
- Problem: Which statement about the time complexity of checking if two strings are anagrams is most efficient and correct?