📖 Scenario: You work at a delivery company. Each delivery job has a deadline and a profit if completed on time. You want to schedule jobs to maximize total profit without missing deadlines.
🎯 Goal: Build a simple program to schedule jobs by their deadlines and calculate the maximum profit possible.
📋 What You'll Learn
Create a list of jobs with exact id, deadline, and profit
Create a variable to track the maximum deadline
Implement a scheduling logic to pick jobs maximizing profit without missing deadlines
Print the scheduled jobs and total profit
💡 Why This Matters
🌍 Real World
Scheduling tasks or deliveries to maximize profit or efficiency is common in logistics, manufacturing, and computing.
💼 Career
Understanding job scheduling algorithms helps in roles like software engineering, operations management, and data analysis.
Progress0 / 4 steps
1
Create the list of jobs
Create a vector called jobs containing these exact tuples: (1, 4, 20), (2, 1, 10), (3, 1, 40), (4, 1, 30). Each tuple is (id, deadline, profit).
DSA Rust
Hint
Use let jobs = vec![(1, 4, 20), (2, 1, 10), (3, 1, 40), (4, 1, 30)];
2
Find the maximum deadline
Create a variable called max_deadline that stores the highest deadline value from the jobs vector.
DSA Rust
Hint
Use jobs.iter().map(|job| job.1).max().unwrap_or(0) to find the max deadline.
3
Schedule jobs to maximize profit
Sort jobs by profit in descending order. Create a vector slots of size max_deadline filled with None. For each job, try to assign it to the latest free slot before its deadline. Use scheduled_jobs vector to store assigned job ids and total_profit to sum profits.
DSA Rust
Hint
Sort jobs by profit descending. Use a vector of Option to track slots. Assign jobs starting from their deadline backwards.
4
Print scheduled jobs and total profit
Print the scheduled job ids in order as Scheduled jobs: [id1, id2, ...] and print the total profit as Total profit: value.
DSA Rust
Hint
Use println!("Scheduled jobs: {:?}", scheduled_jobs); and println!("Total profit: {}", total_profit);
Practice
(1/5)
1. What is the main goal of the Job Scheduling with Deadlines problem?
easy
A. Schedule jobs in the order they arrive without considering deadlines
B. Minimize the number of jobs scheduled
C. Maximize total profit by scheduling jobs before their deadlines
D. Maximize the number of jobs scheduled regardless of profit
Solution
Step 1: Understand the problem goal
The problem asks to schedule jobs so that each job finishes before its deadline and the total profit is maximized.
Step 2: Identify the objective
Maximizing total profit means choosing jobs with higher profit and fitting them before their deadlines.
Final Answer:
Maximize total profit by scheduling jobs before their deadlines -> Option C
Quick Check:
Goal = Maximize profit before deadlines [OK]
Hint: Focus on profit maximization within deadlines [OK]
Common Mistakes:
Thinking the goal is to schedule all jobs
Ignoring deadlines when scheduling
Trying to minimize jobs instead of maximizing profit
2. Which Rust data structure is best suited to keep track of free time slots when implementing job scheduling with deadlines?
easy
A. Vec (vector) with boolean flags for slots
B. HashMap with job IDs as keys
C. LinkedList of jobs sorted by profit
D. BinaryHeap for storing jobs by deadline
Solution
Step 1: Identify the need for tracking free slots
We need to know which time slots are free to schedule jobs before their deadlines.
Step 2: Choose a data structure for slot tracking
A vector of booleans can mark each slot as free or occupied efficiently.
Final Answer:
Vec (vector) with boolean flags for slots -> Option A
Quick Check:
Use Vec<bool> to track free slots [OK]
Hint: Use a boolean vector to mark free/occupied slots [OK]
Common Mistakes:
Using HashMap which is unnecessary for fixed slots
Using LinkedList which is inefficient for slot checking
Using BinaryHeap which is for priority, not slot tracking
3. What is the output of the following Rust code snippet that schedules jobs with deadlines and profits?
struct Job { id: char, deadline: usize, profit: usize }
fn job_scheduling(jobs: &mut Vec<Job>) -> Vec<char> {
jobs.sort_by(|a, b| b.profit.cmp(&a.profit));
let max_deadline = jobs.iter().map(|j| j.deadline).max().unwrap_or(0);
let mut slots = vec![false; max_deadline];
let mut result = Vec::new();
for job in jobs.iter() {
for slot in (0..job.deadline).rev() {
if !slots[slot] {
slots[slot] = true;
result.push(job.id);
break;
}
}
}
result
}
fn main() {
let mut jobs = vec![
Job { id: 'A', deadline: 2, profit: 100 },
Job { id: 'B', deadline: 1, profit: 19 },
Job { id: 'C', deadline: 2, profit: 27 },
Job { id: 'D', deadline: 1, profit: 25 },
Job { id: 'E', deadline: 3, profit: 15 },
];
let scheduled = job_scheduling(&mut jobs);
println!("{:?}", scheduled);
}
Step 2: Schedule jobs in latest free slot before deadline
Schedule A at slot 1 (0-based), C at slot 0, E at slot 2. D and B cannot be scheduled as slots 0 and 1 are taken.
Final Answer:
['A', 'C', 'E'] -> Option D
Quick Check:
Jobs scheduled = ['A', 'C', 'E'] [OK]
Hint: Sort by profit, assign latest free slot before deadline [OK]
Common Mistakes:
Scheduling jobs in arrival order instead of profit order
Assigning earliest slot instead of latest free slot
Ignoring slot availability and deadlines
4. Identify the bug in this Rust code snippet for job scheduling with deadlines:
fn schedule_jobs(jobs: &mut Vec<(char, usize, usize)>) -> Vec<char> {
jobs.sort_by(|a, b| b.2.cmp(&a.2));
let max_deadline = jobs.iter().map(|j| j.1).max().unwrap_or(0);
let mut slots = vec![false; max_deadline];
let mut result = Vec::new();
for job in jobs.iter() {
for slot in 0..job.1 {
if !slots[slot] {
slots[slot] = true;
result.push(job.0);
break;
}
}
}
result
}
medium
A. Jobs are sorted by profit ascending instead of descending
B. Inner loop should iterate slots in reverse order to assign latest slot
C. Result vector should store job profits instead of IDs
D. Slots vector size is incorrect, should be max_deadline + 1
Solution
Step 1: Check sorting order
Jobs are sorted by profit descending (b.2.cmp(&a.2)), which is correct to prioritize high-profit jobs.
Step 2: Check slot assignment loop
The loop iterates from 0 to deadline-1, assigning the earliest available slot. The correct approach assigns the latest free slot before the deadline to leave earlier slots for future jobs with tighter deadlines, so the loop should be reversed using (0..job.1).rev().
Final Answer:
Inner loop should iterate slots in reverse order to assign latest slot -> Option B
Quick Check:
Assign latest free slot by looping backwards [OK]
Hint: Loop slots backward to assign latest free slot [OK]
Common Mistakes:
Assigning earliest slot instead of latest free slot
Using wrong slot vector size (off-by-one)
Storing wrong data in result vector
5. Given jobs with deadlines and profits: A(4, 70), B(1, 80), C(1, 30), D(2, 100), E(3, 40), how many jobs can be scheduled to maximize profit and what is the total profit?
Use the job scheduling with deadlines approach.
Step 2: Schedule jobs in latest free slot before deadline
Schedule B at slot 0 (deadline 1), D at slot 1 (deadline 2), E at slot 2 (deadline 3), A at slot 3 (deadline 4). C cannot be scheduled as slot 0 is taken.
Step 3: Calculate total profit
Profits: B(80) + D(100) + E(40) + A(70) = 290
Final Answer:
4 jobs, total profit 290 -> Option A
Quick Check:
4 jobs scheduled, profit = 290 [OK]
Hint: Sort by profit, assign latest free slot before deadline [OK]
Common Mistakes:
Scheduling fewer jobs than possible
Ignoring slot availability and deadlines
Adding profits of unscheduled jobs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.