0
0
DSA Goprogramming~10 mins

Sorting Stability and When to Use Which Sort in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to perform a stable sort on the slice of integers.

DSA Go
sort.SliceStable(numbers, func(i, j int) bool { return numbers[i] [1] numbers[j] })
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' causes descending order, which is not the intended stable ascending sort.
Using '==' does not define a strict ordering and causes runtime errors.
2fill in blank
medium

Complete the code to perform an unstable sort on the slice of integers.

DSA Go
sort.Slice(numbers, func(i, j int) bool { return numbers[i] [1] numbers[j] })
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' can cause unexpected behavior in unstable sorts.
Using '==' is invalid for sorting comparison.
3fill in blank
hard

Fix the error in the comparison function to ensure a stable sort by age in ascending order.

DSA Go
sort.SliceStable(people, func(i, j int) bool { return people[i].Age [1] people[j].Age })
Drag options to blanks, or click blank then click option'
A>
B>=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' sorts in descending order, which is not the requirement.
Using '==' does not define a strict order and causes errors.
4fill in blank
hard

Fill both blanks to create a map of names to ages for people older than 30.

DSA Go
result := map[string]int{person.Name: person.[1] for _, person := range people if person.Age [2] 30}
Drag options to blanks, or click blank then click option'
AAge
BName
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Name' instead of 'Age' for the map value.
Using '<=' instead of '>' in the condition.
5fill in blank
hard

Fill all four blanks to create a filtered map of uppercase names to ages for people younger than 25.

DSA Go
result := map[string]int{strings.[1](person.[2]): person.[3] for _, person := range people if person.Age [4] 25}
Drag options to blanks, or click blank then click option'
AToUpper
BName
CAge
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ToLower' instead of 'ToUpper'.
Using '>' instead of '<' in the condition.