Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA Typescript
function swap(arr: number[], i: number, j: number): void {
const temp = arr[i];
arr[i] = arr[[1]];
arr[j] = temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j causes no change.
Using arr[j] instead of index causes error.
✗ Incorrect
We swap arr[i] with arr[j], so arr[i] = arr[j].
2fill in blank
mediumComplete the code to move the 'low' pointer forward after swapping.
DSA Typescript
if (arr[mid] < pivot) { swap(arr, low, mid); [1]++; mid++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing mid twice causes skipping elements.
Incrementing pivot or high is incorrect here.
✗ Incorrect
After swapping with low, we move low forward to next position.
3fill in blank
hardFix the error in the condition to check if element equals pivot.
DSA Typescript
else if (arr[mid] [1] pivot) { mid++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' causes assignment error.
Using '==' may cause unexpected type coercion.
✗ Incorrect
Use '===' to check equality in TypeScript for value and type.
4fill in blank
hardFill both blanks to correctly move the 'high' pointer and swap elements greater than pivot.
DSA Typescript
else { swap(arr, mid, [1]); [2]--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mid' instead of 'high' causes infinite loop.
Not decrementing 'high' pointer causes wrong partition.
✗ Incorrect
Swap with 'high' pointer and then move 'high' backward.
5fill in blank
hardFill all three blanks to complete the main loop condition and pointer initialization.
DSA Typescript
let low = 0, mid = 0, high = arr.length [1] 1; while (mid [2] high) { const pivot = arr[[3]]; // partition logic here }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to length causes out-of-bounds error.
Using '>' instead of '<=' in loop condition skips elements.
✗ Incorrect
High starts at last index (length - 1), loop runs while mid <= high, pivot is arr[0].
