Challenge - 5 Problems
Job Scheduling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Job Scheduling with Deadlines - Simple Case
What is the output of the following Go code that schedules jobs with deadlines to maximize profit?
DSA Go
package main import ( "fmt" "sort" ) type Job struct { id string deadline int profit int } func jobScheduling(jobs []Job) []string { sort.Slice(jobs, func(i, j int) bool { return jobs[i].profit > jobs[j].profit }) maxDeadline := 0 for _, job := range jobs { if job.deadline > maxDeadline { maxDeadline = job.deadline } } slots := make([]string, maxDeadline) for i := range slots { slots[i] = "" } for _, job := range jobs { for d := job.deadline - 1; d >= 0; d-- { if slots[d] == "" { slots[d] = job.id break } } } result := []string{} for _, slot := range slots { if slot != "" { result = append(result, slot) } } return result } func main() { jobs := []Job{ {"a", 2, 100}, {"b", 1, 19}, {"c", 2, 27}, {"d", 1, 25}, {"e", 3, 15}, } result := jobScheduling(jobs) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Sort jobs by profit descending, then assign each job to the latest free slot before its deadline.
✗ Incorrect
The algorithm sorts jobs by profit descending: a(100), c(27), d(25), b(19), e(15). It assigns 'a' to slot 1 (deadline 2), 'c' to slot 0 (deadline 2), and 'e' to slot 2 (deadline 3). 'd' and 'b' cannot be scheduled because slots are taken.
🧠 Conceptual
intermediateMaximum Number of Jobs Scheduled
Given a list of jobs with deadlines and profits, what is the maximum number of jobs that can be scheduled without missing deadlines?
Attempts:
2 left
💡 Hint
Think about how many time slots are available to schedule jobs.
✗ Incorrect
The maximum number of jobs that can be scheduled is limited by the maximum deadline because each job must be completed before or on its deadline, and only one job can be scheduled per time slot.
🔧 Debug
advancedIdentify the Bug in Job Scheduling Code
What error will the following Go code produce when scheduling jobs with deadlines?
DSA Go
package main import ( "fmt" "sort" ) type Job struct { id string deadline int profit int } func jobScheduling(jobs []Job) []string { sort.Slice(jobs, func(i, j int) bool { return jobs[i].profit < jobs[j].profit }) maxDeadline := 0 for _, job := range jobs { if job.deadline > maxDeadline { maxDeadline = job.deadline } } slots := make([]string, maxDeadline) for i := range slots { slots[i] = "" } for _, job := range jobs { for d := job.deadline - 1; d >= 0; d-- { if slots[d] == "" { slots[d] = job.id break } } } result := []string{} for _, slot := range slots { if slot != "" { result = append(result, slot) } } return result } func main() { jobs := []Job{ {"a", 2, 100}, {"b", 1, 19}, {"c", 2, 27}, {"d", 1, 25}, {"e", 3, 15}, } result := jobScheduling(jobs) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check the sorting order of jobs by profit.
✗ Incorrect
The code sorts jobs by ascending profit (lowest first), which leads to scheduling low-profit jobs first and missing higher profit jobs, resulting in suboptimal output.
🚀 Application
advancedScheduling Jobs with Overlapping Deadlines
Given jobs with overlapping deadlines, which set of jobs maximizes total profit when scheduled?
Attempts:
2 left
💡 Hint
Consider sorting jobs by profit and scheduling greedily.
✗ Incorrect
Scheduling jobs by descending profit and placing each in the latest available slot before its deadline maximizes total profit, even with overlapping deadlines.
❓ Predict Output
expertOutput of Job Scheduling with Complex Deadlines and Profits
What is the output of the following Go code that schedules jobs with deadlines and profits?
DSA Go
package main import ( "fmt" "sort" ) type Job struct { id string deadline int profit int } func jobScheduling(jobs []Job) []string { sort.Slice(jobs, func(i, j int) bool { return jobs[i].profit > jobs[j].profit }) maxDeadline := 0 for _, job := range jobs { if job.deadline > maxDeadline { maxDeadline = job.deadline } } slots := make([]string, maxDeadline) for i := range slots { slots[i] = "" } for _, job := range jobs { for d := job.deadline - 1; d >= 0; d-- { if slots[d] == "" { slots[d] = job.id break } } } result := []string{} for _, slot := range slots { if slot != "" { result = append(result, slot) } } return result } func main() { jobs := []Job{ {"j1", 4, 70}, {"j2", 1, 80}, {"j3", 1, 30}, {"j4", 2, 100}, {"j5", 3, 40}, {"j6", 2, 20}, } result := jobScheduling(jobs) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Sort jobs by profit descending and assign each to the latest free slot before its deadline.
✗ Incorrect
Jobs sorted by profit: j4(100), j2(80), j1(70), j5(40), j3(30), j6(20). Scheduling: j4 at slot 1, j2 at slot 0, j1 at slot 3, j5 at slot 2. j3 and j6 cannot be scheduled.
