Challenge - 5 Problems
Activity Selection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Activity Selection with Overlapping Activities
What is the output of the following Go code that selects the maximum number of non-overlapping activities?
DSA Go
package main import "fmt" func activitySelection(start []int, end []int) []int { n := len(start) selected := []int{0} lastEnd := end[0] for i := 1; i < n; i++ { if start[i] >= lastEnd { selected = append(selected, i) lastEnd = end[i] } } return selected } func main() { start := []int{1, 3, 0, 5, 8, 5} end := []int{2, 4, 6, 7, 9, 9} result := activitySelection(start, end) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Remember that activities must be sorted by their finish times for the greedy approach to work correctly.
✗ Incorrect
The code assumes activities are sorted by their end times. It selects the first activity, then picks the next activity whose start time is not less than the last selected activity's end time. The selected indices are 0, 1, 3, and 4.
🧠 Conceptual
intermediateKey Idea Behind Activity Selection Problem
Which of the following best describes the main idea behind the greedy solution to the Activity Selection Problem?
Attempts:
2 left
💡 Hint
Think about how to leave room for as many activities as possible.
✗ Incorrect
The greedy approach picks the activity that finishes earliest to maximize the remaining time for other activities.
🔧 Debug
advancedIdentify the Bug in Activity Selection Implementation
What is the bug in this Go code snippet for the Activity Selection Problem?
DSA Go
func activitySelection(start []int, end []int) []int { n := len(start) selected := []int{0} lastEnd := end[0] for i := 1; i < n; i++ { if start[i] > lastEnd { selected = append(selected, i) lastEnd = end[i] } } return selected }
Attempts:
2 left
💡 Hint
Check the condition that allows selecting an activity starting exactly when the last one ends.
✗ Incorrect
Using '>' excludes activities that start exactly when the previous one ends, which is allowed. The condition must be '>='.
🚀 Application
advancedMaximum Number of Activities Selected
Given the start times [2, 1, 3, 0, 5, 8, 5] and end times [3, 2, 4, 6, 7, 9, 9], how many activities can be selected without overlapping?
Attempts:
2 left
💡 Hint
Sort activities by their finish times and select greedily.
✗ Incorrect
After sorting by end times, the selected activities are indices 1, 0, 2, 4, and 5, totaling 5.
📝 Syntax
expertSyntax Error in Activity Selection Go Code
Which option contains a syntax error in this Go code snippet for activity selection?
DSA Go
func activitySelection(start []int, end []int) []int { n := len(start) selected := []int{0} lastEnd := end[0] for i := 1; i < n; i++ { if start[i] >= lastEnd { selected = append(selected, i) lastEnd = end[i] } } return selected }
Attempts:
2 left
💡 Hint
Check the placement of the return statement inside the loop.
✗ Incorrect
Option A returns inside the loop, causing a syntax error or premature return.
