Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after deleting the last element from the array?
Consider the following C code that deletes the last element from an array by reducing its size. What will be printed?
DSA C
int arr[5] = {10, 20, 30, 40, 50}; int size = 5; // Delete last element if (size > 0) { size--; } for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n");
Attempts:
2 left
💡 Hint
Deleting the last element means reducing the size by one and printing only up to the new size.
✗ Incorrect
The array elements remain the same in memory, but the size variable is reduced by one. So the loop prints elements from index 0 to size-1, which excludes the last element 50.
❓ Predict Output
intermediate2:00remaining
What happens if we delete from an empty array?
Given the code below, what will be the output?
DSA C
int arr[3] = {1, 2, 3}; int size = 0; if (size > 0) { size--; } printf("Size: %d\n", size);
Attempts:
2 left
💡 Hint
Check the condition before decreasing size.
✗ Incorrect
Since size is 0, the if condition fails and size remains 0. So output is 'Size: 0'.
🔧 Debug
advanced2:30remaining
Why does this code print an extra element after deletion?
The code below tries to delete the last element from the array but prints one extra element. Identify the bug.
DSA C
int arr[4] = {5, 10, 15, 20}; int size = 4; size = size - 1; for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n");
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
Using i <= size causes the loop to run one extra time, printing arr[size], which is beyond the new last element.
❓ Predict Output
advanced2:30remaining
What is the output after multiple deletions at the end?
Given the code below, what will be printed?
DSA C
int arr[6] = {2, 4, 6, 8, 10, 12}; int size = 6; // Delete last element twice for (int i = 0; i < 2; i++) { if (size > 0) { size--; } } for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n");
Attempts:
2 left
💡 Hint
Each deletion reduces size by one, so two deletions reduce size from 6 to 4.
✗ Incorrect
After two deletions, size is 4, so elements at indices 0 to 3 are printed: 2 4 6 8
🧠 Conceptual
expert2:00remaining
What is the time complexity of deleting the last element in a dynamic array?
Consider a dynamic array (like a vector in C++ or ArrayList in Java). What is the time complexity of deleting the last element?
Attempts:
2 left
💡 Hint
Deleting the last element usually just reduces the size counter.
✗ Incorrect
Deleting the last element in a dynamic array is done by reducing the size variable, which is a constant time operation.
