0
0
DSA Goprogramming~10 mins

Why Sorting Matters and How It Unlocks Other Algorithms in DSA Go - Test Your Knowledge

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

Complete the code to sort the slice of integers in ascending order.

DSA Go
package main

import (
	"fmt"
	"sort"
)

func main() {
	numbers := []int{5, 3, 8, 1, 2}
	sort.[1](numbers)
	fmt.Println(numbers)
}
Drag options to blanks, or click blank then click option'
AInts
BSortStrings
CSortFloat64s
DSort
Attempts:
3 left
💡 Hint
Common Mistakes
Using SortStrings instead of Ints causes a type error.
Using SortFloat64s on integer slices is incorrect.
2fill in blank
medium

Complete the code to check if the slice is sorted in ascending order.

DSA Go
package main

import (
	"fmt"
	"sort"
)

func main() {
	numbers := []int{1, 2, 3, 4, 5}
	isSorted := sort.[1](sort.IntSlice(numbers))
	fmt.Println(isSorted)
}
Drag options to blanks, or click blank then click option'
AIsSortedInts
BSorted
CIsSorted
DCheckSorted
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Sorted' instead of 'IsSorted' causes a compile error.
There is no function named 'IsSortedInts' in the sort package.
3fill in blank
hard

Fix the error in the code to sort a slice of strings alphabetically.

DSA Go
package main

import (
	"fmt"
	"sort"
)

func main() {
	words := []string{"banana", "apple", "cherry"}
	sort.[1](words)
	fmt.Println(words)
}
Drag options to blanks, or click blank then click option'
ASortInts
BSortFloat64s
CSort
DStrings
Attempts:
3 left
💡 Hint
Common Mistakes
Using SortInts on strings causes a type error.
Using SortFloat64s on strings is invalid.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths for words longer than 3 characters.

DSA Go
package main

import "fmt"

func main() {
	words := []string{"go", "code", "sort", "data"}
	lengths := make(map[string]int)
	for _, word := range words {
		if len(word) [1] 3 {
			lengths[word] = [2]
		}
	}
	fmt.Println(lengths)
}
Drag options to blanks, or click blank then click option'
A>
Blen(word)
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes wrong filtering.
Using 'word' as map value instead of length.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths for words longer than 2 characters.

DSA Go
package main

import (
	"fmt"
	"strings"
)

func main() {
	words := []string{"go", "code", "sort", "data"}
	lengths := make(map[string]int)
	for _, word := range words {
		if len(word) [1] 2 {
			lengths[strings.[2](word)] = [3]
		}
	}
	fmt.Println(lengths)
}
Drag options to blanks, or click blank then click option'
A>
Blen(word)
CToUpper
DToLower
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes wrong filtering.
Using ToLower instead of ToUpper changes case incorrectly.
Using 'word' instead of 'len(word)' as map value.