📖 Scenario: You are managing a small workshop where several jobs need to be completed. Each job has a deadline and a profit if finished before or on that deadline. You want to schedule jobs to maximize your total profit.
🎯 Goal: Build a simple program to schedule jobs with deadlines to maximize profit. You will create the job list, set a maximum deadline, select jobs to maximize profit, and then print the scheduled jobs.
📋 What You'll Learn
Create an array of jobs with exact id, deadline, and profit
Create a variable for the maximum deadline
Implement the job scheduling logic to select jobs maximizing profit
Print the scheduled jobs in order of their scheduled time slots
💡 Why This Matters
🌍 Real World
Job scheduling is used in factories, computer task management, and project planning to maximize efficiency and profit.
💼 Career
Understanding job scheduling helps in roles like software development, operations management, and systems engineering where task prioritization is key.
Progress0 / 4 steps
1
Create the job list
Create an array called jobs with these exact objects: {id: 'a', deadline: 2, profit: 100}, {id: 'b', deadline: 1, profit: 19}, {id: 'c', deadline: 2, profit: 27}, {id: 'd', deadline: 1, profit: 25}, {id: 'e', deadline: 3, profit: 15}
DSA Typescript
Hint
Use const jobs = [ ... ] with the exact job objects inside.
2
Set the maximum deadline
Create a variable called maxDeadline and set it to 3, which is the maximum deadline among the jobs.
DSA Typescript
Hint
Use const maxDeadline = 3; to set the maximum deadline.
3
Schedule jobs to maximize profit
Sort the jobs array by profit in descending order. Create an array called result of length maxDeadline filled with null. For each job in sorted order, find a free slot from its deadline backwards and assign the job's id to that slot in result if free.
DSA Typescript
Hint
Sort jobs by profit descending. Use a loop to assign each job to the latest free slot before its deadline.
4
Print the scheduled jobs
Print the result array showing scheduled job ids separated by -> . For empty slots, print null.
DSA Typescript
Hint
Use console.log(result.map(x => x === null ? 'null' : x).join(' -> ')) to print the scheduled jobs.
Practice
(1/5)
1. What is the main goal of the Job Scheduling with Deadlines problem?
easy
A. Maximize the number of jobs without considering profit
B. Minimize the number of jobs scheduled
C. Schedule jobs in the order they appear without considering deadlines
D. Maximize the total profit by scheduling jobs before their deadlines
Solution
Step 1: Understand the problem goal
The problem asks to schedule jobs so that total profit is maximized while respecting deadlines.
Step 2: Identify the objective
Scheduling jobs before their deadlines to get maximum profit is the main goal.
Final Answer:
Maximize the total profit by scheduling jobs before their deadlines -> Option D
Quick Check:
Goal = Maximize profit with deadlines [OK]
Hint: Focus on maximizing profit within deadlines [OK]
Common Mistakes:
Ignoring deadlines and scheduling all jobs
Trying to minimize jobs instead of maximizing profit
Scheduling jobs in input order without sorting
2. Which of the following TypeScript code snippets correctly declares a job interface with id, deadline, and profit properties?
easy
A. type Job = { id: string; deadline: string; profit: string }
Step 2: Schedule jobs in latest available slot before deadline
Job 'a' scheduled at slot 1 (deadline 2), 'c' at slot 0 (deadline 2), 'd' cannot fit before deadline 1 (slot 0 taken), 'b' cannot fit, 'e' scheduled at slot 2 (deadline 3).
Final Answer:
['a', 'c', 'e'] -> Option B
Quick Check:
Scheduled jobs = ['a', 'c', 'e'] [OK]
Hint: Sort by profit, assign latest free slot before deadline [OK]
Common Mistakes:
Scheduling jobs in input order without sorting
Assigning earliest slot instead of latest
Ignoring deadline constraints
4. Identify the bug in the following TypeScript function for job scheduling and select the correct fix.
function scheduleJobs(jobs: Job[]): string[] {
jobs.sort((a, b) => a.profit - b.profit);
const result: (string | null)[] = Array(jobs.length).fill(null);
for (const job of jobs) {
for (let j = job.deadline - 1; j >= 0; j--) {
if (result[j] === null) {
result[j] = job.id;
break;
}
}
}
return result.filter(id => id !== null) as string[];
}
medium
A. Sort jobs by profit descending instead of ascending
B. Initialize result array with zeros instead of null
C. Loop j from 0 to job.deadline - 1 instead of reverse
D. Remove the inner loop and assign jobs directly
Solution
Step 1: Analyze sorting order
Jobs are sorted by profit ascending, which schedules low profit jobs first, reducing total profit.
Step 2: Correct sorting order
Sorting by profit descending ensures high profit jobs are scheduled first for maximum profit.
Final Answer:
Sort jobs by profit descending instead of ascending -> Option A
Quick Check:
Profit descending sort fixes scheduling [OK]
Hint: Sort jobs by descending profit to fix scheduling [OK]
Common Mistakes:
Sorting by ascending profit reduces total profit
Changing loop direction incorrectly
Using zero instead of null for empty slots
5. Given the following jobs with deadlines and profits, what is the maximum total profit achievable by scheduling jobs before their deadlines?
Jobs:
- Job1: deadline = 4, profit = 70
- Job2: deadline = 1, profit = 80
- Job3: deadline = 1, profit = 30
- Job4: deadline = 2, profit = 100
- Job5: deadline = 3, profit = 40
Select the correct maximum profit.