Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to delete the element at the middle index of the array.
DSA C++
int mid = size / 2; for (int i = mid; i < size - 1; i++) { arr[i] = arr[[1]]; } size--;
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr[i - 1] instead of arr[i + 1] causes wrong shifting.
Not decreasing the size after deletion.
✗ Incorrect
To delete the middle element, shift all elements after mid one position to the left by assigning arr[i] = arr[i + 1].
2fill in blank
mediumComplete the code to find the middle index correctly for odd-sized arrays.
DSA C++
int mid = (size - 1) [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division.
Not subtracting 1 before division.
✗ Incorrect
The middle index for zero-based arrays is (size - 1) / 2 when size is odd.
3fill in blank
hardFix the error in the loop condition to avoid accessing out-of-bound elements during deletion.
DSA C++
for (int i = mid; i [1] size - 1; i++) { arr[i] = arr[i + 1]; } size--;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out-of-bound access.
Using '>' or '>=' causes the loop not to run.
✗ Incorrect
The loop should run while i < size - 1 to avoid accessing arr[size], which is out of bounds.
4fill in blank
hardFill both blanks to correctly delete the middle element and update the array size.
DSA C++
int mid = size [1] 2; for (int i = mid; i [2] size - 1; i++) { arr[i] = arr[i + 1]; } size--;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division for mid.
Using '>=' or '<=' in loop condition causing errors.
✗ Incorrect
Divide size by 2 to find mid, and loop while i < size - 1 to shift elements correctly.
5fill in blank
hardFill all three blanks to create a function that deletes the middle element from an array.
DSA C++
void deleteMiddle(int arr[], int &size) {
int mid = size [1] 2;
for (int i = mid; i [2] size - 1; i++) {
arr[i] = arr[[3]];
}
size--;
}
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators for mid calculation.
Incorrect loop condition causing out-of-bound errors.
Wrong element shifting causing data loss.
✗ Incorrect
Divide size by 2 to find mid, loop while i < size - 1, and shift elements by assigning arr[i] = arr[i + 1].
