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 k sorted linked lists and need to merge them into one sorted list efficiently. Which approach guarantees the optimal time complexity for this problem?
easy
A. Concatenate all lists into one and then sort the combined list using a comparison-based sort.
B. Use a greedy approach by repeatedly picking the smallest head node among all lists without any auxiliary data structure.
C. Apply a divide and conquer strategy by recursively merging pairs of lists until one remains.
D. Use dynamic programming to store intermediate merged results and build the final list.

Solution

  1. Step 1: Understand the problem constraints

    We have k sorted lists and want to merge them efficiently.
  2. Step 2: Analyze each approach's time complexity

    Greedy without auxiliary structures leads to O(kN) per extraction, concatenation then sorting is O(N log N), dynamic programming is not applicable here. Divide and conquer merges pairs in O(N log k) time, which is optimal.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Divide and conquer merges pairs efficiently [OK]
Hint: Divide and conquer merges pairs in O(N log k) time [OK]
Common Mistakes:
  • Assuming greedy without heap is efficient
  • Thinking sorting concatenated list is optimal
  • Confusing DP applicability here
2. Given the following code to find the 7th super ugly number with primes [2,3,5], what is the returned value?
easy
A. 8
B. 9
C. 10
D. 12

Solution

  1. Step 1: Trace initial heap and ugly list

    Start with ugly=[1], heap=[(2,0,0),(3,1,0),(5,2,0)]. Pop smallest val=2, append 2 -> ugly=[1,2]. Push (2*ugly[1])=4.
  2. Step 2: Continue popping and pushing

    Sequence generated: 1,2,3,4,5,6,8,10,... The 7th super ugly number is 10.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Manual heap trace confirms 7th number is 10 [OK]
Hint: Trace heap pops and pushes carefully to avoid off-by-one errors [OK]
Common Mistakes:
  • Off-by-one in counting ugly numbers
  • Confusing index for ugly array
  • Skipping duplicates incorrectly
3. Given the following Python code to find the nth ugly number, what is the output when nthUglyNumber(4) is called?
easy
A. 6
B. 5
C. 8
D. 4

Solution

  1. Step 1: Trace heap pops and pushes for n=4

    Initial heap: [1]. Iteration 1 pops 1, pushes 2,3,5. Iteration 2 pops 2, pushes 4,6,10. Iteration 3 pops 3, pushes 6,9,15 (6 already in seen, skip). Iteration 4 pops 4, pushes 8,12,20.
  2. Step 2: Identify the 4th popped ugly number

    The popped numbers in order are 1,2,3,4. The 4th ugly number popped is 4, so the output is 4.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    4 is the 4th ugly number [OK]
Hint: Trace heap pops carefully to find nth ugly number [OK]
Common Mistakes:
  • Off-by-one error in loop iterations
  • Confusing popped value with pushed values
4. Consider the following buggy code for finding the nth ugly number. Which line contains the subtle bug that causes incorrect results or performance issues?
medium
A. Line 3: heap initialization with [1]
B. Line 4: Missing initialization of the 'seen' set
C. Line 7: Loop runs n times popping from heap
D. Line 10: Multiplying ugly by factors 2, 3, and 5

Solution

  1. Step 1: Identify missing duplicate check

    The 'seen' set is not initialized and the check to avoid pushing duplicates is missing, causing duplicates to be pushed repeatedly.
  2. Step 2: Understand impact of missing 'seen'

    Without 'seen', the heap grows unnecessarily large with duplicates, causing performance degradation and possibly incorrect nth number due to repeated values.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Missing 'seen' set causes duplicates and performance issues [OK]
Hint: Always track seen elements to avoid duplicates in heap [OK]
Common Mistakes:
  • Forgetting to initialize or use 'seen'
  • Assuming heap automatically avoids duplicates
5. Suppose the problem is modified so that elements from nums2 can be reused multiple times in pairs (i.e., pairs can reuse the same element from nums2 multiple times). Which modification to the min-heap approach correctly handles this variant?
hard
A. Push all pairs (nums1[i] + nums2[j]) for all i, j into the heap initially and pop k times.
B. After popping a pair (i, j), push (i, j+1) and also push (i+1, j) if within bounds, allowing reuse of nums2 elements.
C. After popping a pair (i, j), push (i, j) again to allow reuse of nums2[j].
D. Modify the heap to store sums with counts of usage and only push new pairs when usage count is less than k.

Solution

  1. Step 1: Understand reuse requirement

    Allowing reuse of nums2 elements means after using nums2[j], we can also try nums2[j+1] and nums2[j] again with next nums1 element.
  2. Step 2: Modify heap push logic

    After popping (i, j), pushing (i, j+1) continues along nums2, and pushing (i+1, j) allows pairing next nums1 element with the same nums2[j], enabling reuse.
  3. Step 3: Reject other options

    Push all pairs (nums1[i] + nums2[j]) for all i, j into the heap initially and pop k times. is brute force and inefficient. After popping a pair (i, j), push (i, j) again to allow reuse of nums2[j]. causes infinite loops by pushing same pair repeatedly. Modify the heap to store sums with counts of usage and only push new pairs when usage count is less than k. is unnecessarily complex and not standard.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    After popping a pair (i, j), push (i, j+1) and also push (i+1, j) if within bounds, allowing reuse of nums2 elements. correctly expands pairs to allow reuse without infinite loops [OK]
Hint: Push next nums1 with same nums2 to allow reuse [OK]
Common Mistakes:
  • Pushing same pair repeatedly causing infinite loops
  • Brute forcing all pairs initially
  • Overcomplicating usage counts
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