Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the map for storing numbers and their indices.
DSA Go
numsMap := map[int]int[1]Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of curly braces {}
Using parentheses () which are for functions
Using angle brackets <> which are not valid in Go
✗ Incorrect
In Go, to create an empty map, you use the map literal `map[keyType]valueType{}`.
2fill in blank
mediumComplete the code to check if the complement exists in the map.
DSA Go
if idx, ok := numsMap[[1]]; ok {
Attempts:
3 left
💡 Hint
Common Mistakes
Adding instead of subtracting to find complement
Reversing the subtraction order
✗ Incorrect
The complement is the number needed to reach the target, so it is target minus the current number.
3fill in blank
hardFix the error in the code to add the current number and its index to the map.
DSA Go
numsMap[[1]] = iAttempts:
3 left
💡 Hint
Common Mistakes
Using the index as the key instead of the number
Using the target instead of the current number
✗ Incorrect
We store the current number as the key and its index as the value in the map.
4fill in blank
hardFill both blanks to complete the for loop iterating over the numbers with index.
DSA Go
for [1], [2] := range nums {
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping index and value variables
Using non-descriptive variable names
✗ Incorrect
In Go, the for range loop returns index and value, commonly named i and num for clarity.
5fill in blank
hardFill both blanks to return the indices of the two numbers that add up to the target.
DSA Go
return []int[1], [2]
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting square brackets around the slice
Returning values in wrong order
✗ Incorrect
In Go, to return a slice with two indices, use the slice literal []int{idx, i}.
