Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after deleting middle element from array
What is the output of the following C code after deleting the middle element from the array?
DSA C
#include <stdio.h> void deleteMiddle(int arr[], int *n) { int mid = *n / 2; for (int i = mid; i < *n - 1; i++) { arr[i] = arr[i + 1]; } (*n)--; } int main() { int arr[] = {10, 20, 30, 40, 50}; int n = 5; deleteMiddle(arr, &n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Remember that deleting the middle element means shifting elements after the middle one to the left.
✗ Incorrect
The middle index for an array of size 5 is 2 (0-based). The element at index 2 (30) is removed, and elements after it (40, 50) shift left. The new array is 10, 20, 40, 50.
❓ Predict Output
intermediate2:00remaining
Resulting array size after middle deletion
What is the size of the array after deleting the middle element using the code below?
DSA C
void deleteMiddle(int arr[], int *n) { int mid = *n / 2; for (int i = mid; i < *n - 1; i++) { arr[i] = arr[i + 1]; } (*n)--; } int main() { int arr[] = {1, 2, 3, 4, 5, 6}; int n = 6; deleteMiddle(arr, &n); printf("%d", n); return 0; }
Attempts:
2 left
💡 Hint
Deleting one element reduces the size by one.
✗ Incorrect
Initially, the array size is 6. After deleting the middle element (index 3), the size decreases by 1 to 5.
🔧 Debug
advanced2:00remaining
Identify the error in middle deletion code
What error will occur when running this code to delete the middle element?
DSA C
void deleteMiddle(int arr[], int *n) { int mid = (*n + 1) / 2; for (int i = mid; i < *n; i++) { arr[i] = arr[i + 1]; } (*n)--; } int main() { int arr[] = {5, 10, 15, 20, 25}; int n = 5; deleteMiddle(arr, &n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Check the loop boundary and array indexing carefully.
✗ Incorrect
The loop runs from mid to n, and accesses arr[i + 1]. When i = n - 1, arr[i + 1] accesses arr[n], which is out of bounds causing runtime error.
❓ Predict Output
advanced2:00remaining
Output after deleting middle element from even-sized array
What is the output of this code after deleting the middle element from an even-sized array?
DSA C
#include <stdio.h> void deleteMiddle(int arr[], int *n) { int mid = (*n - 1) / 2; for (int i = mid; i < *n - 1; i++) { arr[i] = arr[i + 1]; } (*n)--; } int main() { int arr[] = {2, 4, 6, 8, 10, 12}; int n = 6; deleteMiddle(arr, &n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
For even length, middle is the lower middle index.
✗ Incorrect
For n=6, mid = (6-1)/2 = 2. Element at index 2 (6) is deleted. Elements after index 2 shift left. Result: 2 4 8 10 12.
🧠 Conceptual
expert2:00remaining
Time complexity of deleting middle element in array
What is the time complexity of deleting the middle element from an array of size n by shifting elements?
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved after deletion.
✗ Incorrect
Deleting the middle element requires shifting roughly half the elements, which is proportional to n, so time complexity is O(n).
