Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the last element from the array?
Consider the following C++ code that deletes the last element from an array. What will be printed?
DSA C++
int arr[] = {10, 20, 30, 40, 50}; int n = 5; // Delete last element n = n - 1; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
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 initially has 5 elements. After reducing the size by one, the last element (50) is ignored. The loop prints elements from index 0 to 3, which are 10, 20, 30, and 40.
❓ Predict Output
intermediateWhat happens if we delete the last element twice?
Given the code below, what will be the output after deleting the last element two times?
DSA C++
int arr[] = {5, 15, 25, 35, 45}; int n = 5; // Delete last element twice n = n - 1; n = n - 1; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Each deletion reduces the size by one, so two deletions reduce size by two.
✗ Incorrect
Starting with 5 elements, deleting twice reduces size to 3. The loop prints elements at indices 0, 1, and 2: 5, 15, and 25.
🔧 Debug
advancedWhy does this code cause an error when deleting the last element?
Identify the error in the code below that tries to delete the last element from an array and explain why it happens.
DSA C++
int arr[] = {1, 2, 3}; int n = 3; // Delete last element n = n - 1; for (int i = 0; i <= n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Check the loop condition carefully after reducing the size.
✗ Incorrect
After reducing n to 2, the loop runs from i = 0 to i = 2 inclusive (i <= n). This tries to access arr[2], which is valid, but since n was reduced to 2, the last valid index is 1. Actually, the original array has indices 0,1,2. After deletion, n=2 means size is 2, so valid indices are 0 and 1. Loop condition i <= n runs i=0,1,2 which accesses arr[2] which is out of the new size bounds. This causes out-of-bounds access.
❓ Predict Output
advancedWhat is the output after deleting the last element from a dynamic array?
Consider this C++ code using a vector. What will be printed after deleting the last element?
DSA C++
#include <iostream> #include <vector> int main() { std::vector<int> v = {100, 200, 300, 400}; v.pop_back(); for (int x : v) { std::cout << x << " "; } return 0; }
Attempts:
2 left
💡 Hint
pop_back() removes the last element from the vector.
✗ Incorrect
The vector initially has 4 elements. pop_back() removes the last element (400). The loop prints the remaining elements: 100, 200, 300.
🧠 Conceptual
expertWhat is the time complexity of deleting the last element from an array vs a linked list?
Compare the time complexity of deleting the last element from a fixed-size array and a singly linked list. Which statement is correct?
Attempts:
2 left
💡 Hint
Think about how you access the last element in each data structure.
✗ Incorrect
In an array, deleting the last element is just reducing the size, which is O(1). In a singly linked list, to delete the last node, you must traverse from the head to the node before last, which takes O(n) time.
