Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start backtracking from the first position.
DSA Go
func backtrack(pos int) {
if pos == len(nums) {
fmt.Println(path)
return
}
for i := 0; i < len(nums); i++ {
if used[i] {
continue
}
used[i] = true
path = append(path, nums[i])
backtrack([1])
path = path[:len(path)-1]
used[i] = false
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pos instead of pos+1 causes infinite recursion.
Using i as position confuses index with recursion depth.
✗ Incorrect
We move to the next position by increasing pos by 1 to explore further choices.
2fill in blank
mediumComplete the code to mark the element as unused after backtracking.
DSA Go
used[i] = true path = append(path, nums[i]) backtrack(pos + 1) path = path[:len(path)-1] [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark used[i] false causes missing permutations.
Marking used[i] true again causes no progress.
✗ Incorrect
After exploring with nums[i], we mark it unused to allow other branches to use it.
3fill in blank
hardFix the error in the base case condition to stop recursion correctly.
DSA Go
if [1] == len(nums) { fmt.Println(path) return }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of pos causes wrong stopping condition.
Using len(path) can work but is less clear than pos.
✗ Incorrect
The recursion depth is tracked by pos, so we stop when pos reaches length of nums.
4fill in blank
hardFill both blanks to create a decision tree node with current path and explore next level.
DSA Go
node := TreeNode{
value: [1],
children: []TreeNode{},
}
for i := 0; i < len(nums); i++ {
if !used[i] {
used[i] = true
path = append(path, nums[i])
node.children = append(node.children, [2])
path = path[:len(path)-1]
used[i] = false
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using path directly causes all nodes to share same slice.
Calling backtrack with pos causes infinite loops.
✗ Incorrect
We copy current path for node value and recursively build children by calling backtrack with next position.
5fill in blank
hardFill all three blanks to build a map of paths with their lengths filtered by length > 2.
DSA Go
result := map[string]int{
[1]: [2],
}
filtered := map[string]int{}
for k, v := range result {
if v [3] 2 {
filtered[k] = v
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key names causes map errors.
Using wrong comparison operator filters incorrectly.
✗ Incorrect
We use "path" as key, length of path as value, and filter where length is greater than 2.
