Solve interval deletion and GCD subarray problems
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in combinatorial algorithms, specifically interval covering and GCD-based subarray optimization. It tests the ability to design efficient near-linearithmic solutions for classic greedy and number-theory problems commonly assessed in software engineering interviews.
Fewest Interval Deletions So One Interval Covers the Rest
Constraints
- 0 <= n <= 10^5
- Each interval is [l, r] with l <= r (degenerate point intervals l == r allowed)
- Endpoints may exceed 32-bit range; coordinate-compress right endpoints
- Intervals may be identical, nested, or disjoint
Examples
Input: ([[1, 10], [2, 3], [4, 5], [6, 7]],)
Expected Output: 0
Explanation: [1,10] already covers [2,3],[4,5],[6,7], so no deletions are needed.
Input: ([[1, 2], [3, 4], [5, 6]],)
Expected Output: 2
Explanation: All three are pairwise disjoint; the best container holds only itself, so keep 1 and delete 2.
Hints
- Minimizing deletions equals maximizing kept intervals under one chosen container. Fix a container [L, R]; what must every other kept interval satisfy? (l >= L and r <= R.)
- Counting how many intervals a container dominates is a two-sided 2D dominance count. Eliminate one inequality by sorting on the left endpoint, and answer the other with a Fenwick/BIT prefix query over compressed right endpoints.
- Handle ties carefully: insert ALL intervals sharing the same left endpoint into the BIT before querying any of them, so an equal-L interval (including the container itself and exact duplicates) is counted iff its right endpoint <= R.
Shortest Subarray With GCD > 1 After At Most k Changes
Constraints
- 0 <= n <= 10^5
- 1 <= nums[i] (positive integers; may exceed 32-bit — use trial division / Pollard-rho instead of the sieve for very large values)
- 0 <= k <= n
- Return -1 when no valid subarray exists (only when k == 0 and every element equals 1)
Examples
Input: ([2, 2, 4, 9, 6], 1)
Expected Output: 1
Explanation: The single element 2 already has gcd 2 > 1 with zero changes, so the minimum length is 1.
Input: ([6, 10, 15], 0)
Expected Output: 1
Explanation: With k=0, the single element 6 alone has gcd 6 > 1, so length 1 suffices.
Hints
- gcd > 1 over a set means a single prime p divides every element. A changed element can always be made a multiple of p, so changes are free with respect to p — only unchanged non-multiples constrain you.
- You cannot try every prime. A prime is only worth testing if it divides at least one array element; any other prime makes every element a non-multiple, so it only helps windows of length <= k (already covered by the length-1 case when k >= 1). Factor each value with an SPF sieve.
- For each candidate prime, run a two-pointer window counting non-multiples; shrink while the count exceeds k. The shortest feasible window over all primes (and the universal length-1 window when k >= 1) is the answer; impossible only when k == 0 and all elements are 1.