Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort activities by their finish time.
DSA Go
sort.Slice(activities, func(i, j int) bool {
return activities[i].finish [1] activities[j].finish
})Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes sorting in descending order.
Using '==' or '!=' does not sort properly.
✗ Incorrect
We sort activities by their finish time in ascending order to select the earliest finishing activity first.
2fill in blank
mediumComplete the code to select the first activity after sorting.
DSA Go
selected := []Activity{activities[[1]]}Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 skips the first activity.
Using -1 or len(activities) causes runtime errors.
✗ Incorrect
The first activity after sorting is at index 0.
3fill in blank
hardFix the error in the loop condition to select compatible activities.
DSA Go
for i := 1; i < [1]; i++ { if activities[i].start >= selected[len(selected)-1].finish { selected = append(selected, activities[i]) } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'activities' directly causes a compile error.
Using 'selected' limits the loop incorrectly.
Using cap(activities) may cause incomplete iteration.
✗ Incorrect
We loop through all activities using their length to avoid index errors.
4fill in blank
hardFill both blanks to define the Activity struct and initialize activities slice.
DSA Go
type Activity struct {
start int
[1] int
}
activities := []Activity{{
{start: 1, [2]: 4},
{start: 3, finish: 5},
{start: 0, finish: 6},
}}Attempts:
3 left
💡 Hint
Common Mistakes
Using different field names causes compile errors.
Using 'end' or 'duration' instead of 'finish' breaks the logic.
✗ Incorrect
The field name must be 'finish' to match usage in code and initialization.
5fill in blank
hardFill all three blanks to print the selected activities' start and finish times.
DSA Go
for _, act := range selected { fmt.Println("Activity:", [1], "to", [2]) } // To print the number of selected activities fmt.Println("Total selected:", [3])
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'selected' instead of 'len(selected)' prints the slice, not count.
Mixing up start and finish fields in print statements.
✗ Incorrect
We print start and finish fields of each activity and the total count using len(selected).
