Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the slice.
DSA Go
arr[i], arr[i+1] = arr[i+1], arr[[1]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index like i+1 or i-1 in the swap.
Forgetting that both sides of the assignment must match the swapped elements.
✗ Incorrect
To swap elements at positions i and i+1, we assign arr[i+1] to arr[i] and arr[i] to arr[i+1]. The blank should be 'i' to complete the swap.
2fill in blank
mediumComplete the code to run the inner loop for bubble sort.
DSA Go
for j := 0; j < len(arr) - 1 - [1]; j++ {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i in the subtraction.
Not subtracting anything, causing unnecessary comparisons.
✗ Incorrect
The inner loop runs fewer times each outer loop iteration, so we subtract i to avoid checking already sorted elements.
3fill in blank
hardFix the error in the comparison to swap elements if needed.
DSA Go
if arr[j] [1] arr[j+1] {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which would sort in descending order.
Using '==' or '!=' which do not help sorting.
✗ Incorrect
In bubble sort, we swap if the current element is greater than the next to sort in ascending order.
4fill in blank
hardFill both blanks to complete the outer loop and initialize the slice length.
DSA Go
n := len(arr) for [1] := 0; [2] < n-1; [2]++ {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop control.
Using 'j' which is usually for inner loop.
✗ Incorrect
The outer loop variable is usually 'i' and it runs from 0 to n-2, so both blanks should be 'i'.
5fill in blank
hardFill all three blanks to complete the bubble sort function.
DSA Go
func bubbleSort(arr []int) {
n := len(arr)
for [1] := 0; [1] < n-1; [1]++ {
for [2] := 0; [2] < n-1-[1]; [2]++ {
if arr[[2]] [3] arr[[2]+1] {
arr[[2]], arr[[2]+1] = arr[[2]+1], arr[[2]]
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing loop variable names.
Using wrong comparison operator.
✗ Incorrect
The outer loop uses 'i', inner loop uses 'j', and swap happens if arr[j] > arr[j+1].