📖 Scenario: Imagine you are organizing a small party and want to invite friends. Each friend has a different set of available days. You want to find a combination of friends who can all come on the same day. This problem shows why simple greedy choices might fail and how backtracking helps find the right solution.
🎯 Goal: You will create a program that tries to find a day when all selected friends can come together. You will first set up the data, then configure a target day, apply backtracking to find a valid group, and finally print the result.
📋 What You'll Learn
Create a map of friends with their available days
Set a target day to check for availability
Use backtracking to find a group of friends available on the target day
Print the list of friends who can come on that day
💡 Why This Matters
🌍 Real World
Scheduling meetings or events where participants have different available times requires checking all possibilities to find a common slot.
💼 Career
Understanding backtracking helps in solving complex problems like scheduling, puzzles, and constraint satisfaction where greedy methods fail.
Progress0 / 4 steps
1
Set up friends' availability
Create a map called friends where keys are friend names (strings) and values are slices of strings representing available days. Use these exact entries: "Alice": {"Monday", "Tuesday"}, "Bob": {"Tuesday", "Wednesday"}, "Charlie": {"Monday", "Wednesday"}.
DSA Go
Hint
Use a map with string keys and slice of strings as values. Each friend has a list of days.
2
Set the target day
Create a string variable called targetDay and set it to "Tuesday".
DSA Go
Hint
Just create a string variable with the exact name and value.
3
Find friends available on the target day using backtracking
Write a function called availableFriends that takes the friends map and targetDay string, and returns a slice of strings with names of friends who have targetDay in their availability. Use a loop to check each friend and their days. Call this function inside main and store the result in result.
DSA Go
Hint
Loop over each friend and their days. If a day matches targetDay, add the friend to the result slice.
4
Print the friends available on the target day
Use fmt.Println to print the result slice inside main.
DSA Go
Hint
Use fmt.Println(result) to show the list of friends.
Practice
(1/5)
1. Why do we need backtracking instead of just using a greedy approach for some problems?
easy
A. Because backtracking only works for sorting problems.
B. Because backtracking always runs faster than greedy algorithms.
C. Because greedy algorithms use more memory than backtracking.
D. Because greedy algorithms make local choices that may not lead to the best overall solution.
Solution
Step 1: Understand greedy algorithm behavior
Greedy algorithms pick the best option at each step without reconsidering previous choices.
Step 2: Understand backtracking advantage
Backtracking tries all possibilities and can undo choices to find the best overall solution.
Final Answer:
Because greedy algorithms make local choices that may not lead to the best overall solution. -> Option D
Quick Check:
Greedy local choice ≠ global best [OK]
Hint: Backtracking explores all options; greedy picks locally best only [OK]
Common Mistakes:
Thinking greedy is always faster and better
Confusing memory use between algorithms
Believing backtracking is only for sorting
2. Which of the following Go code snippets correctly starts a backtracking function signature for solving a problem?
easy
A. func backtrack(path []int, choices []int) { }
B. func backtrack(path int, choices int) { }
C. func backtrack() int { }
D. func backtrack(path string) string { }
Solution
Step 1: Identify typical backtracking parameters
Backtracking usually passes the current path (partial solution) and remaining choices as slices.
Step 2: Check each function signature
func backtrack(path []int, choices []int) { } uses slices for path and choices, suitable for backtracking. Others use single values or wrong types.
Final Answer:
func backtrack(path []int, choices []int) { } -> Option A
Quick Check:
Backtracking uses slices for path and choices [OK]
Hint: Backtracking functions usually take slices for path and choices [OK]
Common Mistakes:
Using single int instead of slices
Returning values when not needed
Using wrong parameter types
3. What is the output of this Go code snippet that tries to solve the subset sum problem using backtracking?
package main
import "fmt"
func backtrack(nums []int, target int, start int, path []int) {
if target == 0 {
fmt.Println(path)
return
}
for i := start; i < len(nums); i++ {
if nums[i] > target {
continue
}
backtrack(nums, target-nums[i], i+1, append(path, nums[i]))
}
}
func main() {
nums := []int{2, 3, 6, 7}
backtrack(nums, 7, 0, []int{})
}
medium
A. [2 3 6]
[7]
B. [2 3 7]
[7]
C. [7]
D. [7]
[2 3 6]
Solution
Step 1: Understand the problem and code
The code finds subsets of nums that sum to 7, using backtracking and no repeats.
Step 2: Trace the backtracking calls
Possible subsets: [7] sums to 7; [2,3,6] sums to 11 (too big), so skipped; only [7] prints.
4. Find the bug in this Go backtracking code snippet that tries to generate all permutations of a slice of integers:
func permute(nums []int, path []int, used []bool) {
if len(path) == len(nums) {
fmt.Println(path)
return
}
for i := 0; i < len(nums); i++ {
if used[i] {
continue
}
path = append(path, nums[i])
used[i] = true
permute(nums, path, used)
used[i] = false
}
}
medium
A. Missing removal of last element from path after recursion
B. Used slice is not initialized
C. Base case condition is incorrect
D. Loop index should start from 1
Solution
Step 1: Analyze backtracking recursion steps
After recursive call, path must remove last added element to backtrack correctly.
Step 2: Identify missing backtracking step
Code appends to path but never removes last element after recursion, causing incorrect paths.
Final Answer:
Missing removal of last element from path after recursion -> Option A
Quick Check:
Backtracking needs to undo path changes [OK]
Hint: Always undo changes to path after recursive call [OK]
Common Mistakes:
Forgetting to remove last element from path
Not initializing used slice before call
Incorrect base case length check
5. You want to solve the classic N-Queens problem where you place N queens on an NxN chessboard so that no two queens attack each other. Why can't a greedy algorithm solve this correctly, and how does backtracking help?
hard
A. Greedy only works for even N; backtracking works for all N.
B. Greedy places queens row by row without checking future conflicts; backtracking tries all placements and backtracks on conflicts.
C. Greedy uses too much memory; backtracking uses less memory by pruning.
D. Greedy sorts queens by position; backtracking sorts by attack count.
Solution
Step 1: Understand greedy limitations for N-Queens
Greedy tries to place queens row by row choosing first safe spot, but may miss solutions requiring backtracking.
Step 2: Understand backtracking approach
Backtracking tries all possible queen placements, undoing choices when conflicts arise, ensuring all solutions are found.
Final Answer:
Greedy places queens row by row without checking future conflicts; backtracking tries all placements and backtracks on conflicts. -> Option B
Quick Check:
N-Queens needs full search, not greedy local choice [OK]
Hint: N-Queens needs full search; greedy fails on future conflicts [OK]
Common Mistakes:
Thinking greedy can always find a solution
Confusing memory use differences
Believing greedy works only for even N
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.