Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize an empty map to count character frequencies.
DSA Go
freq := map[rune]int[1]Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of curly braces {}
Using parentheses () which are for function calls
Using angle brackets <> which are not valid in Go
✗ Incorrect
In Go, to create an empty map, use the map literal `map[rune]int{}`.
2fill in blank
mediumComplete the code to loop over each character in the string 'text'.
DSA Go
for _, ch := [1] text {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'of' which is not valid in Go
Using 'on' or 'over' which are not Go keywords
✗ Incorrect
In Go, to loop over elements in a string or slice, use 'for _, value := range collection'.
3fill in blank
hardFix the error in the code to increment the frequency count of character 'ch'.
DSA Go
freq[ch] [1] freq[ch] + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which is a comparison operator
Using '+=' which is invalid for map elements
Using ':=' which declares a new variable
✗ Incorrect
To update a map value, assign the new value with '='. '+=' does not work directly on map elements.
4fill in blank
hardFill both blanks to print each character and its frequency from the map.
DSA Go
for ch, count := [1] freq { fmt.[2]("%c: %d\n", ch, count) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Println' which does not support format specifiers
Using 'Print' which prints without formatting
Using 'for ch, count := freq' which is invalid syntax
✗ Incorrect
Use 'range' to iterate over map keys and values. Use 'Printf' to format output with characters and numbers.
5fill in blank
hardFill all three blanks to create a map of character frequencies from a string 'input'.
DSA Go
func charFreq(input string) map[rune]int {
freq := map[rune]int[1]
for _, ch := [2] input {
freq[ch] [3] freq[ch] + 1
}
return freq
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'of' instead of 'range' in the loop
Using '+=' instead of '=' for map assignment
Not initializing the map properly
✗ Incorrect
Initialize map with '{}', loop with 'range', and assign increment with '='.
