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
Focus on handling minimal and boundary inputs correctly before tackling edge cases.
Understand common algorithmic pitfalls like greedy traps and exact boundary conditions.
Optimize your solution using appropriate data structures like min-heaps to handle large inputs efficiently.
t1_01basic
Input{"K":2,"jobs":[[0,5],[1,2],[5,3]]}
Expected[0,1,0]
⏱ Performance - must finish in 2000ms
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.
💡 Consider tracking when each machine becomes free to assign jobs efficiently.
💡 Use two min-heaps: one for available machines by index, one for running jobs by finish time.
💡 Assign jobs by earliest available machine; if multiple free, pick smallest index.
Why it failed: Output incorrect: likely not updating machine availability correctly or not choosing smallest index machine when multiple are free. Fix by maintaining two min-heaps and always assign job to earliest available machine with smallest index.
✓ Correct assignment of jobs to machines with earliest availability and smallest index.
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.
💡 Track machine availability times and assign jobs accordingly.
💡 When multiple machines free at job start, pick smallest index.
💡 Update machine free time after assigning each job.
Why it failed: Wrong output due to ignoring machine index priority when multiple machines free simultaneously. Fix by using min-heap keyed by machine index for available machines.
✓ Correctly handles multiple machines and job assignments respecting earliest availability and machine index.
t2_01edge
Input{"K":1,"jobs":[[0,5],[1,3],[4,2]]}
Expected[0,0,0]
⏱ Performance - must finish in 2000ms
Only one machine; all jobs assigned sequentially to machine 0 regardless of overlap.
💡 Test behavior when only one machine is available.
💡 Ensure jobs are queued and assigned sequentially on the single machine.
💡 Machine availability time must be updated after each job.
Why it failed: Incorrect output due to not queuing jobs properly on single machine or ignoring machine availability updates. Fix by updating machine free time after each job.
✓ Correctly assigns all jobs sequentially to single machine.
t2_02edge
Input{"K":3,"jobs":[[0,2],[0,3],[0,1],[0,4]]}
Expected[0,1,2,0]
⏱ Performance - must finish in 2000ms
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.
💡 Check handling of jobs with identical start times.
💡 Assign jobs to machines with smallest index first when multiple free.
💡 Update machine availability correctly for jobs starting simultaneously.
Why it failed: Wrong output due to ignoring machine index priority for jobs with same start time. Fix by using min-heap keyed by machine index for available machines.
✓ Correctly assigns jobs with same start time respecting machine index priority.
t2_03edge
Input{"K":5,"jobs":[]}
Expected[]
⏱ Performance - must finish in 2000ms
No jobs to assign; output is empty list.
💡 Test behavior with no jobs.
💡 Function should handle empty input gracefully.
💡 Return empty list when no jobs.
Why it failed: Function crashes or returns non-empty output on empty jobs input. Fix by checking for empty jobs list and returning empty result.
✓ Correctly returns empty list when no jobs.
t2_04edge
Input{"K":4,"jobs":[[0,0],[0,0],[0,0],[0,0]]}
Expected[0,1,2,3]
⏱ Performance - must finish in 2000ms
Jobs with zero duration assigned to machines 0,1,2,3; machines become free immediately after assignment.
💡 Test handling of zero duration jobs.
💡 Ensure machine availability updates correctly even if duration is zero.
💡 Machines should be immediately available after zero duration jobs.
Why it failed: Fails zero duration test by not updating machine availability or causing infinite wait. Fix by updating machine free time to start + duration even if duration is zero.
✓ Correctly handles zero duration jobs and machine availability updates.
t3_01corner
Input{"K":2,"jobs":[[0,5],[1,2],[2,3]]}
Expected[0,1,1]
⏱ Performance - must finish in 2000ms
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.
💡 Beware of greedy approach that assigns job 2 to machine 0 just because it has smaller index.
💡 Check machine availability times carefully before assignment.
💡 Assign job to machine that becomes free earliest, not just smallest index.
Why it failed: Fails greedy trap: assigning job 2 to machine 0 ignoring that machine 1 frees earlier. Fix by comparing machine availability times before assignment.
Jobs assigned sequentially to machine 0 as it becomes free exactly at job start times.
💡 Check boundary condition when machine frees exactly at job start time.
💡 Ensure machine is considered free if free time <= job start time.
💡 Update machine availability correctly to allow immediate reuse.
Why it failed: Fails exact boundary test by requiring machine free time < job start time instead of <=. Fix by allowing assignment when machine free time equals job start time.
✓ Correctly handles exact boundary condition for machine availability.
t3_03corner
Input{"K":3,"jobs":[[0,0],[0,0],[0,0]]}
Expected[0,1,2]
⏱ Performance - must finish in 2000ms
Jobs with zero duration assigned to machines 0,1,2; machines become free immediately after assignment.
💡 Test handling of zero duration jobs.
💡 Ensure machine availability updates correctly even if duration is zero.
💡 Machines should be immediately available after zero duration jobs.
Why it failed: Fails zero duration test by not updating machine availability or causing infinite wait. Fix by updating machine free time to start + duration even if duration is zero.
✓ Correctly handles zero duration jobs and machine availability updates.
t4_01performance
Input{"K":100000,"jobs":[]}
Expectednull
⏱ 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.
💡 Brute force O(n*K) approach will time out.
💡 Use two min-heaps to track available and running machines efficiently.
💡 Assign jobs by earliest available machine using heaps to achieve O(n log K).
Why it failed: TLE due to O(n*K) brute force approach. Fix by implementing min-heap based solution with O(n log K) complexity.
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
Step 1: Understand problem constraints
The problem requires finding the k smallest pairs by sum from two sorted arrays efficiently.
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.
Final Answer:
Option C -> Option C
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
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.
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.
Final Answer:
Option A -> Option A
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
Step 1: Analyze countPairs complexity
countPairs runs in O(n + m) because j only moves backward once across all i.
Step 2: Analyze binary search iterations
Binary search runs O(log(maxSum - minSum)) times.
Final Answer:
Option A -> Option A
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
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.
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).
Final Answer:
Option A -> Option A
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
Step 1: Understand reuse implications
Allowing reuse means the same element can be counted multiple times, so indices alone do not track usage.
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.
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.