Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the Job class with id, deadline, and profit fields.
DSA Java
class Job { int id; int deadline; int [1]; Job(int id, int deadline, int profit) { this.id = id; this.deadline = deadline; this.profit = profit; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'time' or 'duration' instead of 'profit' for the profit field.
✗ Incorrect
The Job class needs a 'profit' field to store the profit value for each job.
2fill in blank
mediumComplete the code to sort the jobs array by profit in descending order.
DSA Java
Arrays.sort(jobs, (a, b) -> b.[1] - a.profit);Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by deadline or id instead of profit.
✗ Incorrect
We sort by 'profit' to schedule jobs with higher profit first.
3fill in blank
hardFix the error in the loop that tries to find a free slot for a job before its deadline.
DSA Java
for (int j = Math.min(maxDeadline, jobs[i].deadline) - 1; j >= 0; j--) { if (slot[j] == [1]) { result[j] = i; slot[j] = true; break; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if slot[j] is true instead of false.
✗ Incorrect
We check if the slot is free by seeing if slot[j] is false (not taken).
4fill in blank
hardFill both blanks to initialize the slot array and result array for scheduling.
DSA Java
boolean[] slot = new boolean[[1]]; int[] result = new int[[2]]; for (int i = 0; i < [1]; i++) { slot[i] = false; result[i] = -1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using jobs.length or maxProfit for array sizes.
✗ Incorrect
Both arrays should be sized by maxDeadline to cover all possible slots.
5fill in blank
hardFill all three blanks to print the scheduled jobs in order with their profits.
DSA Java
System.out.print("Scheduled jobs: "); for (int i = 0; i < [1]; i++) { if (result[i] != -1) { System.out.print(jobs[result[i]].[2] + "(" + jobs[result[i]].[3] + ") "); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping over jobs.length instead of maxDeadline.
Printing wrong fields like deadline.
✗ Incorrect
We loop up to maxDeadline and print job id and profit for scheduled jobs.
