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 first element from array
What will be the output of the following C code after deleting the first element from the array?
DSA C
int arr[] = {10, 20, 30, 40, 50}; int n = 5; // Delete first element by shifting for (int i = 0; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }
Attempts:
2 left
💡 Hint
After deleting the first element, all elements shift left by one position.
✗ Incorrect
The code shifts all elements left by one, overwriting the first element. The size reduces by one, so the output prints elements from index 0 to n-1, which are 20, 30, 40, 50.
❓ Predict Output
intermediate1:00remaining
Resulting array size after deletion
Given an array of size 7, after deleting the first element by shifting, what is the new size of the array?
DSA C
int arr[7] = {5, 10, 15, 20, 25, 30, 35}; int n = 7; // Delete first element for (int i = 0; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--;
Attempts:
2 left
💡 Hint
Deleting one element reduces the size by one.
✗ Incorrect
The size variable n is decremented by one after deletion, so new size is 6.
🔧 Debug
advanced2:00remaining
Identify the bug in array deletion code
What is the bug in the following code that deletes the first element of an array?
DSA C
int arr[] = {1, 2, 3, 4, 5}; int n = 5; for (int i = 0; i <= n - 1; i++) { arr[i] = arr[i + 1]; } n--; for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }
Attempts:
2 left
💡 Hint
Check the loop boundary to avoid accessing out of bounds.
✗ Incorrect
The loop runs one step too far, accessing arr[n] which is out of bounds. It should run till i < n - 1 to avoid this.
❓ Predict Output
advanced2:00remaining
Output after multiple deletions at beginning
What is the output after deleting the first element twice from the array?
DSA C
int arr[] = {100, 200, 300, 400, 500}; int n = 5; // Delete first element twice for (int d = 0; d < 2; d++) { for (int i = 0; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; } for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }
Attempts:
2 left
💡 Hint
Each deletion shifts elements left and reduces size by one.
✗ Incorrect
After first deletion, array is 200,300,400,500; after second deletion, 300,400,500 remain.
🧠 Conceptual
expert1:30remaining
Time complexity of deleting first element in array
What is the time complexity of deleting the first element from an array of size n by shifting all elements left by one?
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved.
✗ Incorrect
Deleting the first element requires shifting all remaining n-1 elements left, so time complexity is linear O(n).
