0
0
DSA Goprogramming~10 mins

Count Words with Given Prefix 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 check if a word starts with the given prefix.

DSA Go
if strings.[1](word, prefix) { return true } else { return false }
Drag options to blanks, or click blank then click option'
AHasPrefix
BContains
CSplit
DIndex
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings.Contains instead of strings.HasPrefix
Using strings.Index which returns position, not boolean
2fill in blank
medium

Complete the code to iterate over all words in the list.

DSA Go
for [1] := 0; i < len(words); i++ { word := words[i] }
Drag options to blanks, or click blank then click option'
Ai
Bx
Ck
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable different from the one in the loop condition
Using a variable not declared in the loop
3fill in blank
hard

Fix the error in the code to correctly count words with the prefix.

DSA Go
count := 0
for _, word := range words {
    if strings.HasPrefix(word, prefix) {
        count [1] 1
    }
}
return count
Drag options to blanks, or click blank then click option'
A=
B-=
C+=
D*=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which overwrites count
Using '-=' which decreases count
4fill in blank
hard

Fill both blanks to create a map of words and their prefix match status.

DSA Go
result := make(map[string]bool)
for _, [1] := range words {
    result[word] = strings.[2](word, prefix)
}
return result
Drag options to blanks, or click blank then click option'
Aword
Bi
CHasPrefix
DContains
Attempts:
3 left
💡 Hint
Common Mistakes
Using index variable instead of word
Using strings.Contains instead of strings.HasPrefix
5fill in blank
hard

Fill all three blanks to build a prefix count map for a list of words.

DSA Go
prefixCount := make(map[string]int)
for _, word := range words {
    for i := 1; i <= len(word); i++ {
        prefix := word[:[1]]
        prefixCount[prefix] [2] [3] 1
    }
}
return prefixCount
Drag options to blanks, or click blank then click option'
Ai
B+=
C+
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong slice index
Using '=' instead of '+=' for map update
Using '-' instead of '+' for increment