Bird
Raised Fist0
Interview Prepk-way-mergehardAmazonGoogle

Meeting Rooms III (K Machines, Earliest Available)

Choose your preparation mode4 modes available
</>
IDE
def assign_jobs(K: int, jobs: list[list[int]]) -> list[int]:public int[] assignJobs(int K, int[][] jobs)vector<int> assignJobs(int K, vector<vector<int>>& jobs)function assignJobs(K, jobs)
def assign_jobs(K, jobs):
    # Write your solution here
    pass
class Solution {
    public int[] assignJobs(int K, int[][] jobs) {
        // Write your solution here
        return new int[0];
    }
}
#include <vector>
using namespace std;

vector<int> assignJobs(int K, vector<vector<int>>& jobs) {
    // Write your solution here
    return {};
}
function assignJobs(K, jobs) {
    // Write your solution here
}
Coming soon
0/10
Common Bugs to Avoid
Wrong: [0, 0, 0]Assigning all jobs to machine 0 without considering machine availability or other machines.✅ Use min-heaps to track machine availability and assign jobs to earliest available machine, not always machine 0.
Wrong: [0, 1, 0]Assigning job to machine with smallest index ignoring machine availability times (greedy trap).✅ Compare machine availability times and assign to machine that frees earliest, breaking ties by smallest index.
Wrong: [0, 1, 2, 3]Not reusing machines when they become free exactly at job start time (boundary condition error).✅ Allow assignment if machine free time <= job start time, not strictly less.
Wrong: [0, 0, 0]Failing to handle zero duration jobs properly, causing incorrect machine availability updates.✅ Update machine free time to start + duration even if duration is zero to avoid infinite wait or overlap.
Wrong: Timeout or no outputUsing brute force O(n*K) approach for large inputs causing TLE.✅ Implement min-heap based approach with O(n log K) complexity to handle large inputs efficiently.
✓
Test Cases
t1_01basic
Input{"K":2,"jobs":[[0,5],[1,2],[5,3]]}
Expected[0,1,0]

Job 0 starts at 0 on machine 0. Job 1 starts at 1 on machine 1. Job 2 starts at 5; machine 0 is free at 5, so assign job 2 to machine 0.

t1_02basic
Input{"K":3,"jobs":[[0,3],[1,9],[2,6],[3,1],[10,2]]}
Expected[0,1,2,0,0]

Jobs assigned to machines 0,1,2 initially. Job 3 starts at 3; machine 0 is free at 3, so assigned to 0. Job 4 starts at 10; machine 0 free at 4, assigned to 0.

t2_01edge
Input{"K":1,"jobs":[[0,5],[1,3],[4,2]]}
Expected[0,0,0]

Only one machine; all jobs assigned sequentially to machine 0 regardless of overlap.

t2_02edge
Input{"K":3,"jobs":[[0,2],[0,3],[0,1],[0,4]]}
Expected[0,1,2,0]

All jobs start at same time 0; assign to machines 0,1,2 in order; last job waits for machine 0 to free at 2, then assigned.

t2_03edge
Input{"K":5,"jobs":[]}
Expected[]

No jobs to assign; output is empty list.

t2_04edge
Input{"K":4,"jobs":[[0,0],[0,0],[0,0],[0,0]]}
Expected[0,1,2,3]

Jobs with zero duration assigned to machines 0,1,2,3; machines become free immediately after assignment.

t3_01corner
Input{"K":2,"jobs":[[0,5],[1,2],[2,3]]}
Expected[0,1,1]

Job 0 assigned to machine 0. Job 1 assigned to machine 1. Job 2 starts at 2; machine 1 is free at 3 (after job 1), machine 0 free at 5; assign job 2 to machine 1.

t3_02corner
Input{"K":3,"jobs":[[0,2],[2,2],[4,2],[6,2]]}
Expected[0,0,0,0]

Jobs assigned sequentially to machine 0 as it becomes free exactly at job start times.

t3_03corner
Input{"K":3,"jobs":[[0,0],[0,0],[0,0]]}
Expected[0,1,2]

Jobs with zero duration assigned to machines 0,1,2; machines become free immediately after assignment.

t4_01performance
Input{"K":100000,"jobs":[]}
⏱ Performance - must finish in 2000ms

Large input with n=100000 jobs and K=100000 machines; solution must run in O(n log K) time within 2 seconds.

Practice

(1/5)
1. You are given two sorted arrays and need to find the k pairs with the smallest sums formed by taking one element from each array. Which approach guarantees an optimal solution with efficient time complexity?
easy
A. Use dynamic programming to build a table of sums and select the smallest pairs from it.
B. Use a brute force approach by enumerating all pairs, then sorting them to find the smallest sums.
C. Use a min-heap to lazily generate pairs starting from the smallest possible sums, expanding only as needed.
D. Use a greedy approach by always pairing the smallest elements from each array without backtracking.

Solution

  1. Step 1: Understand problem constraints

    The problem requires finding the k smallest pairs by sum from two sorted arrays efficiently.
  2. Step 2: Evaluate approaches

    Brute force enumerates all pairs, which is inefficient. DP is not suitable since pairs are not independent subproblems. Greedy fails because pairing smallest elements once misses other smaller sums. The min-heap approach lazily expands pairs starting from the smallest sums, ensuring optimality and efficiency.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Min-heap approach matches the known optimal solution [OK]
Hint: Min-heap lazily expands pairs for optimal efficiency [OK]
Common Mistakes:
  • Assuming greedy pairing always works
  • Thinking DP applies directly
  • Believing brute force is efficient enough
2. You are given k sorted lists of integers. Your task is to find the smallest range that includes at least one number from each of the k lists. Which algorithmic approach guarantees an optimal solution with efficient time complexity?
easy
A. Use a min-heap to track the current minimum element among the heads of each list while maintaining the current maximum, updating the range as you iterate.
B. Apply a greedy approach by always picking the smallest element from any list and expanding the range until all lists are covered.
C. Sort all elements from all lists into one array and use a dynamic programming approach to find the minimal range covering all lists.
D. Use a brute force approach enumerating all combinations of elements from each list and selecting the minimal range.

Solution

  1. Step 1: Understand the problem constraints

    The problem requires finding a range that covers at least one element from each of the k sorted lists, minimizing the range size.
  2. Step 2: Identify the optimal approach

    Using a min-heap to track the smallest current element from each list and maintaining the maximum element seen so far allows efficient updates of the range while iterating through the lists.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Min-heap approach efficiently tracks min and max for optimal range [OK]
Hint: Min-heap tracks min and max for optimal range [OK]
Common Mistakes:
  • Thinking brute force is feasible for large k
  • Assuming greedy picking smallest element always works
  • Confusing DP applicability here
3. What is the time complexity of the binary search approach that finds the k-th smallest sum in two sorted arrays of lengths n and m, assuming the sums range between minSum and maxSum?
medium
A. O((n + m) * log(maxSum - minSum))
B. O(n * m * log(maxSum - minSum))
C. O(n * log m * log(maxSum - minSum))
D. O(n * m)

Solution

  1. Step 1: Analyze countPairs complexity

    countPairs runs in O(n + m) because j only moves backward once across all i.
  2. Step 2: Analyze binary search iterations

    Binary search runs O(log(maxSum - minSum)) times.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Combined complexity is O((n + m) * log(maxSum - minSum)) [OK]
Hint: countPairs uses two-pointer linear scan, not nested loops [OK]
Common Mistakes:
  • Assuming countPairs is O(n*m)
  • Confusing n and m in complexity
4. What is the time complexity of the optimal min-heap based algorithm to find the nth super ugly number given k primes?
medium
A. O(n log k) -- because each of the n iterations involves a heap operation on size k
B. O(nk) -- because each of the n ugly numbers is generated by multiplying with k primes
C. O(n log n) -- because the heap size grows with n as new numbers are pushed
D. O(k log n) -- because k primes are processed with log n complexity each

Solution

  1. Step 1: Identify heap size and operations per iteration

    The heap size is fixed at k, as each prime contributes one element at a time.
  2. Step 2: Calculate total complexity

    For n ugly numbers, each iteration pops and pushes one element, each heap operation O(log k), total O(n log k).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Heap size does not grow with n, so complexity depends on k, not n [OK]
Hint: Heap size fixed at k -> O(n log k) time [OK]
Common Mistakes:
  • Assuming heap grows with n, leading to O(n log n)
  • Confusing k and n in complexity
  • Ignoring heap operation cost
5. Suppose the problem is modified so that elements in the lists can be reused multiple times (i.e., you can pick the same element multiple times from the same list). Which modification to the optimal min-heap approach correctly handles this variant?
hard
A. Continue using the min-heap approach but do not advance the index in the list after pushing the next element, allowing reuse of the same element.
B. Use a min-heap but track counts of elements used per list and only advance indices when all elements have been used once.
C. Modify the algorithm to allow pushing the same element multiple times without advancing the index, but update current_max accordingly to avoid infinite loops.
D. The min-heap approach cannot be adapted; instead, use a dynamic programming approach to handle reuse.

Solution

  1. Step 1: Understand reuse implications

    Allowing reuse means the same element can be counted multiple times, so indices alone do not track usage.
  2. Step 2: Modify tracking mechanism

    Tracking counts of usage per list and advancing indices only after all elements are used once prevents infinite loops and ensures coverage.
  3. Step 3: Why other options fail

    Not advancing indices leads to infinite loops; pushing same element multiple times without control breaks correctness; DP is inefficient here.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Tracking usage counts ensures correct reuse handling [OK]
Hint: Track usage counts per list to handle reuse correctly [OK]
Common Mistakes:
  • Not advancing indices causing infinite loops
  • Pushing duplicates without control
  • Switching to DP unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard