Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first index from the end where the array value is less than the next value.
DSA C#
int i = nums.Length - 2; while (i >= 0 && nums[i] [1] nums[i + 1]) { i--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' causes incorrect index selection.
Not decrementing i properly leads to infinite loop.
✗ Incorrect
We look for the first index from the right where nums[i] is less than nums[i+1], so we continue while nums[i] >= nums[i+1].
2fill in blank
mediumComplete the code to find the first index from the end where the array value is greater than nums[i].
DSA C#
int j = nums.Length - 1; while (j >= 0 && nums[j] [1] nums[i]) { j--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' causes skipping the correct index.
Not checking j >= 0 leads to index errors.
✗ Incorrect
We want to find the first index j from the right where nums[j] is greater than nums[i], so we continue while nums[j] <= nums[i].
3fill in blank
hardFix the error in swapping nums[i] and nums[j].
DSA C#
int temp = nums[i];
nums[i] = nums[[1]];
nums[j] = temp;Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with nums[i] again causes no change.
Using wrong index causes incorrect swap.
✗ Incorrect
We swap nums[i] with nums[j], so the second assignment must use nums[j].
4fill in blank
hardFill both blanks to reverse the subarray after index i.
DSA C#
int start = i + 1; int end = nums.Length - 1; while (start [1] end) { int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; start[2]; end--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes no loop execution.
Not incrementing start causes infinite loop.
✗ Incorrect
We reverse the subarray by swapping while start < end, incrementing start and decrementing end.
5fill in blank
hardFill all three blanks to complete the Next Permutation function.
DSA C#
public void NextPermutation(int[] nums) {
int i = nums.Length - 2;
while (i >= 0 && nums[i] [1] nums[i + 1]) {
i--;
}
if (i >= 0) {
int j = nums.Length - 1;
while (j >= 0 && nums[j] [2] nums[i]) {
j--;
}
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
int start = i + 1;
int end = nums.Length - 1;
while (start [3] end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up comparison operators causes wrong permutation.
Not reversing the suffix correctly leads to incorrect output.
✗ Incorrect
The first while uses '>=' to find the pivot, the second uses '<=' to find the swap candidate, and the last uses '<' to reverse the suffix.
