Challenge - 5 Problems
Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Permutations for a 3-Element Array
What is the output of the following Go code that generates all permutations of the array [1, 2, 3]?
DSA Go
package main import ( "fmt" ) func permute(nums []int, start int, result *[][]int) { if start == len(nums)-1 { // Make a copy of nums and append to result temp := make([]int, len(nums)) copy(temp, nums) *result = append(*result, temp) return } for i := start; i < len(nums); i++ { nums[start], nums[i] = nums[i], nums[start] permute(nums, start+1, result) nums[start], nums[i] = nums[i], nums[start] } } func main() { arr := []int{1, 2, 3} var result [][]int permute(arr, 0, &result) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Focus on the order in which the swapping happens and how the recursion explores permutations.
✗ Incorrect
The code swaps elements starting from the current index and recursively generates permutations. The order of permutations is fixed by the swapping sequence, producing option D's output.
🧠 Conceptual
intermediateNumber of Permutations for an Array of Length N
If you have an array of length N, how many unique permutations can you generate?
Attempts:
2 left
💡 Hint
Think about how many choices you have for each position in the array.
✗ Incorrect
The number of unique permutations of an array of length N is N factorial (N!). This is because for the first position you have N choices, for the second N-1, and so on.
🔧 Debug
advancedIdentify the Bug in Permutation Generation Code
What error does the following Go code produce when trying to generate permutations of [1, 2, 3]?
DSA Go
package main import "fmt" func permute(nums []int, start int, result *[][]int) { if start == len(nums) { *result = append(*result, nums) return } for i := start; i < len(nums); i++ { nums[start], nums[i] = nums[i], nums[start] permute(nums, start+1, result) nums[start], nums[i] = nums[i], nums[start] } } func main() { arr := []int{1, 2, 3} var result [][]int permute(arr, 0, &result) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Look at how the slice is appended to the result in the base case.
✗ Incorrect
Appending the slice 'nums' directly stores references to the same underlying array. Later swaps modify all stored slices, making all permutations appear identical.
🚀 Application
advancedFind the 4th Permutation of [1, 2, 3, 4]
Using lexicographic order, what is the 4th permutation of the array [1, 2, 3, 4]?
Attempts:
2 left
💡 Hint
List permutations in order starting from [1 2 3 4].
✗ Incorrect
The lexicographic permutations start as: 1) [1 2 3 4], 2) [1 2 4 3], 3) [1 3 2 4], 4) [1 3 4 2].
📝 Syntax
expertIdentify the Syntax Error in Permutation Code Snippet
Which option contains the syntax error in this Go code snippet for generating permutations?
DSA Go
func permute(nums []int, start int, result *[][]int) { if start == len(nums)-1 { temp := make([]int, len(nums)) copy(temp, nums) *result = append(*result, temp) return } for i := start; i < len(nums); i++ { nums[start], nums[i] = nums[i], nums[start] permute(nums, start+1, result) nums[start], nums[i] = nums[i], nums[start] } }
Attempts:
2 left
💡 Hint
Check if all braces are properly closed.
✗ Incorrect
Option A is correct because the code snippet is missing the final closing brace '}' for the function permute.
