Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting first element from array
What is 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++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
After deleting the first element, all elements shift left by one position.
✗ Incorrect
The first element (10) is removed by shifting all elements left. The new array is {20, 30, 40, 50} with size 4.
🧠 Conceptual
intermediateEffect of deleting first element on array size
If you delete the first element from an array of size 7 by shifting elements left, what will be the new size of the array?
Attempts:
2 left
💡 Hint
Deleting one element reduces the size by one.
✗ Incorrect
Deleting one element reduces the array size by one, so from 7 to 6.
🔧 Debug
advancedIdentify the error in array deletion code
What error does the following code produce when deleting the first element from an array?
int arr[] = {1, 2, 3, 4};
int n = 4;
for (int i = 0; i <= n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
DSA C++
int arr[] = {1, 2, 3, 4}; int n = 4; for (int i = 0; i <= n - 1; i++) { arr[i] = arr[i + 1]; } n--;
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs until i <= n-1, so at i = n-1, arr[i+1] accesses arr[n], which is out-of-bounds causing runtime error.
❓ Predict Output
advancedOutput after multiple deletions at beginning
What is the output after deleting the first element twice from the array below?
DSA C++
int arr[] = {5, 10, 15, 20, 25}; int n = 5; // Delete first element for (int i = 0; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; // Delete first element again for (int i = 0; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Each deletion shifts elements left and reduces size by one.
✗ Incorrect
After first deletion: {10, 15, 20, 25}, size 4. After second deletion: {15, 20, 25}, size 3.
🚀 Application
expertNumber of shifts required to delete first element multiple times
If you delete the first element from an array of size 10, three times in a row by shifting elements left each time, how many total element shifts occur?
Attempts:
2 left
💡 Hint
Each deletion shifts elements equal to current size minus one.
✗ Incorrect
First deletion shifts 9 elements, second shifts 8, third shifts 7. Total = 9 + 8 + 7 = 24.
