Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort jobs by their deadlines in ascending order.
DSA Typescript
jobs.sort((a, b) => a.deadline [1] b.deadline); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using comparison operators like '<' or '>' directly inside sort callback.
Returning boolean instead of number in sort function.
✗ Incorrect
The sort function expects a negative number if a should come before b. Using 'a.deadline - b.deadline' sorts ascending by deadline.
2fill in blank
mediumComplete the code to find the maximum deadline among all jobs.
DSA Typescript
const maxDeadline = Math.max(...jobs.map(job => job.[1]));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'profit' or other properties instead of 'deadline'.
Not using spread operator with Math.max.
✗ Incorrect
We want the maximum deadline, so we map each job to its deadline property.
3fill in blank
hardFix the error in the code that schedules jobs to maximize profit without missing deadlines.
DSA Typescript
for (let i = 0; i < jobs.length; i++) { let j = Math.min(maxDeadline, jobs[i].deadline) - 1; while (j >= 0 && slot[j] [1] true) { j--; } if (j >= 0) { slot[j] = true; result[j] = jobs[i]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '!==' which would check for false slots incorrectly.
Using assignment '=' instead of comparison.
✗ Incorrect
We check if the slot is already taken (true), so we continue while slot[j] == true.
4fill in blank
hardFill both blanks to complete the function that returns the scheduled jobs in order of their slots.
DSA Typescript
function getScheduledJobs(result: (Job | null)[]): Job[] {
return result.filter(job => job [1] null).[2]((jobA, jobB) => jobA.deadline - jobB.deadline);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!==' in filter.
Using 'map' instead of 'sort' after filtering.
✗ Incorrect
We filter out null jobs using '!== null' and then sort the scheduled jobs by their deadline.
5fill in blank
hardFill all three blanks to complete the job scheduling function that returns the maximum profit and scheduled jobs.
DSA Typescript
function scheduleJobs(jobs: Job[]): { maxProfit: number; scheduledJobs: Job[] } {
jobs.sort((a, b) => b.[1] - a.[2]);
const maxDeadline = Math.max(...jobs.map(job => job.[3]));
const slot: boolean[] = Array(maxDeadline).fill(false);
const result: (Job | null)[] = Array(maxDeadline).fill(null);
let maxProfit = 0;
for (const job of jobs) {
let j = Math.min(maxDeadline, job.deadline) - 1;
while (j >= 0 && slot[j]) { j--; }
if (j >= 0) {
slot[j] = true;
result[j] = job;
maxProfit += job.profit;
}
}
return { maxProfit, scheduledJobs: result.filter(job => job !== null) as Job[] };
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by deadline instead of profit.
Using wrong properties in sort or max calculation.
✗ Incorrect
Sort jobs by profit descending (b.profit - a.profit), find max deadline from job.deadline.