Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a vector of jobs with their deadlines and profits.
DSA Rust
let jobs = vec![("Job1", 2, 100), ("Job2", [1], 19), ("Job3", 2, 27)];
Attempts:
3 left
💡 Hint
Common Mistakes
Using a deadline less than or equal to the first job's deadline.
Confusing profit with deadline.
✗ Incorrect
The second job has a deadline of 3, so the correct value is 3.
2fill in blank
mediumComplete the code to sort jobs by profit in descending order.
DSA Rust
jobs.sort_by(|a, b| b.[1].cmp(&a.[1]));
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index for profit.
Sorting in ascending order instead of descending.
✗ Incorrect
The profit is the third element (index 2) in the tuple, so we compare b.2 with a.2.
3fill in blank
hardFix the error in the code to initialize the result vector with None values for scheduling.
DSA Rust
let mut result: Vec<Option<&str>> = vec![[1]; max_deadline];Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or empty string instead of None.
Using Some("") which is not empty.
✗ Incorrect
We need to initialize the vector with None values to represent empty slots.
4fill in blank
hardFill both blanks to check if a slot is free and assign the job to that slot.
DSA Rust
if result[[1]].is_none() { result[[2]] = Some(job.0); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for checking and assigning.
Using undefined variables.
✗ Incorrect
We use the same variable 'i' to check and assign the slot in the result vector.
5fill in blank
hardFill all three blanks to complete the loop that schedules jobs before their deadlines.
DSA Rust
for job in jobs.iter() { let mut [1] = job.1.min(max_deadline) - 1; while [2] >= 0 { if result[[3]].is_none() { result[[3]] = Some(job.0); break; } [2] -= 1; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the same purpose.
Not decrementing the index correctly.
✗ Incorrect
The variable 'i' is used to track the slot index for scheduling jobs.
