Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA Java
int temp = arr[[1]]; arr[[1]] = arr[j]; arr[j] = temp;
Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i for the first element causes incorrect swapping.
Using arr.length as index causes ArrayIndexOutOfBoundsException.
✗ Incorrect
We swap the element at index i with the element at index j to generate permutations.
2fill in blank
mediumComplete the code to generate permutations recursively by fixing the element at index {{BLANK_1}}.
DSA Java
public void permute(int[] arr, int [1], int n) { if ([1] == n) { printArray(arr); return; } for (int j = [1]; j < n; j++) { swap(arr, [1], j); permute(arr, [1] + 1, n); swap(arr, [1], j); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not consistent with the rest of the code.
Confusing the variable with the array length.
✗ Incorrect
The variable i is commonly used as the current index to fix in the permutation recursion.
3fill in blank
hardFix the error in the base case condition to correctly print the permutation when all positions are fixed.
DSA Java
if (i [1] n) { printArray(arr); return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or greater than causes premature or missed base case.
Using not equal causes infinite recursion.
✗ Incorrect
The base case is when the current index i equals n, meaning all elements are fixed and the permutation is ready to print.
4fill in blank
hardFill both blanks to complete the swap and recursive call inside the loop.
DSA Java
for (int j = [1]; j < n; j++) { swap(arr, [2], j); permute(arr, [2] + 1, n); swap(arr, [2], j); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using j as the first argument in swap causes incorrect swapping.
Using 0 or n as indices causes errors.
✗ Incorrect
The loop starts at i, and swaps elements at i and j. The recursive call fixes the next position i+1.
5fill in blank
hardFill all three blanks to complete the method signature and the base case condition.
DSA Java
public void permute(int[] [1], int i, int [2]) { if (i [3] [2]) { printArray(arr); return; } // rest of code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using length as a parameter name conflicts with array property.
Using incorrect comparison operator in base case.
✗ Incorrect
The array parameter is named arr, the length parameter is named size, and the base case checks if i == size.
