Bird
Raised Fist0
DSA C++programming~20 mins

Array Deletion at End in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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. 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] << " ";
}
A10 20 30 40 50
B50
C10 20 30 40
D10 20 30
Attempts:
2 left
💡 Hint
Deleting the last element means reducing the size by one and printing only up to the new size.
❓ Predict Output
intermediate
2:00remaining
What 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] << " ";
}
A5 15 25 35
B5 15 25
C5 15 25 35 45
D15 25 35
Attempts:
2 left
💡 Hint
Each deletion reduces the size by one, so two deletions reduce size by two.
🔧 Debug
advanced
2:00remaining
Why 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] << " ";
}
AIt causes an out-of-bounds error because the loop runs one index too far.
BIt causes a syntax error due to missing semicolon.
CIt causes a type error because n is not an integer.
DIt causes no error and prints all elements correctly.
Attempts:
2 left
💡 Hint
Check the loop condition carefully after reducing the size.
❓ Predict Output
advanced
2:00remaining
What 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;
}
A400
B100 200 300 400
C100 200
D100 200 300
Attempts:
2 left
💡 Hint
pop_back() removes the last element from the vector.
🧠 Conceptual
expert
2:00remaining
What 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?
ADeleting last element in an array is O(1), in a singly linked list is O(n).
BDeleting last element in an array is O(n), in a singly linked list is O(1).
CDeleting last element in both array and singly linked list is O(n).
DDeleting last element in both array and singly linked list is O(1).
Attempts:
2 left
💡 Hint
Think about how you access the last element in each data structure.

Practice

(1/5)
1. What happens to the size of an array when you delete an element from the end in a dynamic array?
easy
A. The size decreases by one
B. The size remains the same
C. The size increases by one
D. The size becomes zero

Solution

  1. Step 1: Understand dynamic array size behavior

    When an element is deleted from the end, the array size reduces by one because one element is removed.
  2. Step 2: Confirm size change after deletion

    Deleting from the end does not affect other elements, only reduces size by one.
  3. Final Answer:

    The size decreases by one -> Option A
  4. Quick Check:

    Size after deletion = original size - 1 [OK]
Hint: Deleting at end reduces size by one [OK]
Common Mistakes:
  • Thinking size remains same after deletion
  • Assuming size increases after deletion
  • Confusing deletion at end with deletion at start
2. Which of the following is the correct syntax to delete the last element from a vector named vec in C++?
easy
A. vec.erase_end();
B. vec.delete_last();
C. vec.remove_end();
D. vec.pop_back();

Solution

  1. Step 1: Recall vector deletion syntax

    The standard method to remove the last element from a vector in C++ is pop_back().
  2. Step 2: Verify other options

    Other methods like delete_last(), remove_end(), and erase_end() do not exist in C++ STL vector.
  3. Final Answer:

    vec.pop_back(); -> Option D
  4. Quick Check:

    Use pop_back() to delete last element [OK]
Hint: Use pop_back() to remove last element in vector [OK]
Common Mistakes:
  • Using non-existent methods like delete_last()
  • Confusing erase() with erase_end()
  • Trying to use remove_end() which is invalid
3. What will be the output of the following C++ code?
std::vector<int> v = {10, 20, 30, 40};
v.pop_back();
for(int x : v) std::cout << x << " ";
medium
A. 10 20 30 40
B. 20 30 40
C. 10 20 30
D. 10 20 40

Solution

  1. Step 1: Understand pop_back effect on vector

    pop_back() removes the last element, which is 40 in this vector.
  2. Step 2: Print remaining elements

    After removal, vector contains 10, 20, 30. Loop prints these with spaces.
  3. Final Answer:

    10 20 30 -> Option C
  4. Quick Check:

    Last element removed, output = 10 20 30 [OK]
Hint: pop_back() removes last element before printing [OK]
Common Mistakes:
  • Printing original vector without deletion
  • Confusing which element is removed
  • Forgetting pop_back() modifies vector
4. Identify the error in the following code that attempts to delete the last element of an array:
int arr[3] = {1, 2, 3};
arr.pop_back();
medium
A. pop_back() deletes first element, not last
B. pop_back() is not a member of built-in arrays
C. Array size is zero, cannot delete
D. No error, code is correct

Solution

  1. Step 1: Check array type and methods

    Built-in arrays in C++ do not have member functions like pop_back().
  2. Step 2: Understand correct usage

    pop_back() is a method of std::vector, not raw arrays.
  3. Final Answer:

    pop_back() is not a member of built-in arrays -> Option B
  4. Quick Check:

    pop_back() only works with vectors, not arrays [OK]
Hint: pop_back() works only on vectors, not raw arrays [OK]
Common Mistakes:
  • Assuming pop_back() works on raw arrays
  • Ignoring that arrays have fixed size
  • Thinking pop_back() deletes first element
5. You have a vector v with elements {5, 10, 15, 20, 25}. You want to delete the last two elements and then print the vector. Which code snippet correctly does this?
hard
A. v.erase(v.end()-2, v.end()); for(auto x : v) std::cout << x << ' ';
B. v.pop_back(); for(auto x : v) std::cout << x << ' ';
C. v.remove_end(); v.remove_end(); for(auto x : v) std::cout << x << ' ';
D. v.pop_back(2); for(auto x : v) std::cout << x << ' ';

Solution

  1. Step 1: Understand methods to delete multiple elements

    pop_back() removes only one element at a time. To remove two, it must be called twice. erase() can remove a range efficiently.
  2. Step 2: Analyze the snippets

    The snippet v.erase(v.end()-2, v.end()); for(auto x : v) std::cout << x << ' '; correctly removes the last two elements. The snippet with a single v.pop_back(); removes only one element. remove_end() does not exist. pop_back(2) uses invalid syntax.
  3. Final Answer:

    v.erase(v.end()-2, v.end()); for(auto x : v) std::cout << x << ' '; -> Option A
  4. Quick Check:

    erase() with range removes last two elements efficiently [OK]
Hint: Use erase(end()-2, end()) to delete last two elements [OK]
Common Mistakes:
  • Using pop_back() only once instead of twice
  • Using non-existent remove_end() method
  • Not knowing erase() can remove ranges
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard