Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
Problem: n ≤ 10⁵ tasks with durations and deadlines; complete as many tasks as possible, one at a time, each finishing by its deadline. Which greedy works?
- A. Sort by duration and take while time allows
- B. Sort by deadline and skip any task that does not fit
- C. Sort by deadline; add each task to a max-heap of durations, and if total time exceeds the deadline, drop the longest task taken so far – O(n log n)
- D. Take tasks in input order
Correct answer: C. Sort by deadline; add each task to a max-heap of durations, and if total time exceeds the deadline, drop the longest task taken so far – O(n log n)
Processing by deadline keeps feasibility checks simple; when the schedule overflows, removing the longest task frees the most time while losing only one task – an exchange argument shows this is optimal. Simply skipping the current task can be worse than removing an earlier long one; sorting by duration ignores deadlines.
More Coding Interview Problems questions
- Problem: Find the median of two sorted arrays of sizes m and n (up to 10⁶ each) in better than linear time. What is the optimal complexity?
- Problem: A social graph has V = 10⁶ users and E = 10⁷ friendships. You must store it to run BFS. Which representation is appropriate and…
- Problem: Given 'a/b = k' equations (e.g. a/b = 2, b/c = 3), answer queries like a/c. Which model and algorithm fit?
- Problem: Evaluate division queries on the ratio graph a/b = 2, b/c = 3, d/e = 4. python from collections import defaultdict g =…
- Problem: Reverse a word after prepending a marker using Java's StringBuilder. java StringBuilder sb = new StringBuilder("interview")…
- Problem: Find the longest substring in which every character appears at least k times (divide and conquer on characters that are too…