Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the arrays.
DSA Java
int temp = arr1[[1]]; arr1[[1]] = arr2[j]; arr2[j] = temp;
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index variable for arr1.
Swapping with an incorrect element from arr2.
✗ Incorrect
We swap elements at index i in arr1 with element at index j in arr2 to maintain order.
2fill in blank
mediumComplete the code to compare elements from arr1 and arr2.
DSA Java
if (arr1[i] > arr2[[1]]) { // swap logic }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr1[i] with arr1[j] instead of arr2[j].
Using an incorrect index for arr2.
✗ Incorrect
We compare arr1[i] with arr2[j] to decide if swapping is needed.
3fill in blank
hardFix the error in the loop condition to iterate over arr1 correctly.
DSA Java
for (int i = 0; i < [1]; i++) { // loop body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr2.length which may be different.
Using length - 1 which misses the last element.
✗ Incorrect
We loop through all elements of arr1 using arr1.length as the limit.
4fill in blank
hardFill both blanks to complete the insertion sort step for arr2.
DSA Java
int key = arr2[[1]]; int j = [2] - 1; while (j >= 0 && arr2[j] > key) { arr2[j + 1] = arr2[j]; j--; } arr2[j + 1] = key;
Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i for key index.
Starting j from wrong index.
✗ Incorrect
We pick the element at index i in arr2 as key and start comparing from i-1.
5fill in blank
hardFill all three blanks to complete the merge function without extra space.
DSA Java
public static void merge(int[] arr1, int[] arr2) {
int n = arr1.length;
int m = arr2.length;
for (int i = 0; i < n; i++) {
if (arr1[i] > arr2[[1]]) {
int temp = arr1[i];
arr1[i] = arr2[[2]];
arr2[[3]] = temp;
// insertion sort step for arr2
int key = arr2[[2]];
int j = [2] - 1;
while (j >= 0 && arr2[j] > key) {
arr2[j + 1] = arr2[j];
j--;
}
arr2[j + 1] = key;
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing indices i and j incorrectly.
Not updating arr2 properly after swap.
✗ Incorrect
We compare and swap elements at index i in arr1 with index j in arr2, then insert arr2[j] correctly.
