Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort jobs by their profit in descending order.
DSA Javascript
jobs.sort((a, b) => b.[1] - a.profit);Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by deadline instead of profit.
Using ascending order instead of descending.
✗ Incorrect
We sort jobs by their profit property in descending order to maximize total profit.
2fill in blank
mediumComplete the code to find the maximum deadline among all jobs.
DSA Javascript
const maxDeadline = Math.max(...jobs.map(job => job.[1]));
Attempts:
3 left
💡 Hint
Common Mistakes
Using profit instead of deadline.
Not using spread operator with Math.max.
✗ Incorrect
We find the maximum deadline to know the total number of time slots available.
3fill in blank
hardFix the error in the code to assign a job to the latest available slot before its deadline.
DSA Javascript
for (let j = Math.min(maxDeadline, job.[1]) - 1; j >= 0; j--) { if (!slots[j]) { slots[j] = job; break; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using profit instead of deadline in the loop.
Starting loop from wrong index.
✗ Incorrect
We use job.deadline to find the latest slot before the deadline to schedule the job.
4fill in blank
hardFill both blanks to calculate total profit and count of scheduled jobs.
DSA Javascript
let totalProfit = 0; let count = 0; for (const job of slots) { if (job) { totalProfit += job.[1]; count += [2]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding deadline instead of profit.
Incrementing count by 0 or job.deadline.
✗ Incorrect
We add job.profit to totalProfit and increment count by 1 for each scheduled job.
5fill in blank
hardFill the blanks to return the result as an object with count and totalProfit.
DSA Javascript
return { [1]: count, [2]: totalProfit, message: 'Jobs scheduled successfully' };
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys like 'profit' or 'jobsCount' instead of 'count' and 'totalProfit'.
✗ Incorrect
We return an object with keys count and totalProfit, and a message.
