Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA
Problem: Given n ≤ 10⁵ intervals, find the minimum number of points such that every interval contains at least one point. Which greedy rule is correct?
- A. Sort by end; place a point at the end of the first uncovered interval, skip all intervals containing it
- B. Sort by start; place a point at each interval's start
- C. Sort by length; place a point in the middle of the shortest
- D. Place points at every distinct endpoint
Correct answer: A. Sort by end; place a point at the end of the first uncovered interval, skip all intervals containing it
Choosing the earliest end is safe by an exchange argument: any solution must stab that interval somewhere at or before its end, and moving that point right to the end can only cover more. O(n log n). Starting points fail on nested intervals like [1,10], [2,3].
More Coding Interview Problems questions
- Problem: Given n balloons as horizontal intervals, find the minimum number of vertical arrows that burst all of them (a balloon bursts if…
- Problem: Insert a new interval into a sorted list of non-overlapping intervals, merging if necessary. python def insert(iv, new): out, i…
- Problem: n ≤ 10⁵ trains arrive and depart at given minutes. Find the minimum number of platforms. Which approach is O(n log n) and correct?
- Problem: Assign cookies of sizes s to children with greed factors g (a child is content if cookie ≥ greed). Maximise content children…
- Problem: A staircase has n steps; you climb 1 or 2 steps at a time. Count the distinct ways to reach the top. cpp long long ways(int n) {…
- Problem: A thief robs houses in a row but cannot rob two adjacent houses. Find the maximum loot. python def rob(a): take = skip = 0 for x…