Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting elements at the end of an array
What is the output of the following C++ code that inserts elements at the end of an array?
DSA C++
#include <iostream>
using namespace std;

int main() {
    int arr[5] = {1, 2, 3};
    int size = 3;
    int capacity = 5;

    if (size < capacity) {
        arr[size] = 4;
        size++;
    }
    if (size < capacity) {
        arr[size] = 5;
        size++;
    }

    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}
A1 2 3 4 5
B1 2 3 4
C1 2 3
D1 2 3 5
Attempts:
2 left
💡 Hint
Check how many times the insertion condition is true and how size changes.
❓ Predict Output
intermediate
2:00remaining
Resulting array after conditional insertion
What will be the output of this code snippet that tries to insert an element at the end only if there is space?
DSA C++
#include <iostream>
using namespace std;

int main() {
    int arr[4] = {10, 20, 30, 40};
    int size = 4;
    int capacity = 4;

    if (size < capacity) {
        arr[size] = 50;
        size++;
    }

    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}
ARuntime error
B10 20 30 40 50
C10 20 30 40 0
D10 20 30 40
Attempts:
2 left
💡 Hint
Check if the insertion condition is true or false.
🔧 Debug
advanced
2:00remaining
Identify the error in array insertion code
What error will this code produce when trying to insert an element at the end of the array?
DSA C++
#include <iostream>
using namespace std;

int main() {
    int arr[3] = {5, 10, 15};
    int size = 3;
    int capacity = 3;

    arr[size] = 20;
    size++;

    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}
ARuntime error: Array index out of bounds
BCompilation error
C5 10 15 20
D20 10 15 20
Attempts:
2 left
💡 Hint
Check if the code respects the array capacity before insertion.
🧠 Conceptual
advanced
1:30remaining
Understanding array insertion at end with fixed size
Why is it important to check the array capacity before inserting an element at the end?
ATo avoid overwriting existing elements and causing data loss
BTo ensure the array elements remain sorted after insertion
CTo prevent accessing memory outside the array bounds which can cause crashes or bugs
DTo reduce the time complexity of insertion operation
Attempts:
2 left
💡 Hint
Think about what happens if you write beyond the array size in memory.
🚀 Application
expert
2:30remaining
Predict the final array after multiple insertions with capacity check
Given the following code, what is the final printed array?
DSA C++
#include <iostream>
using namespace std;

int main() {
    int arr[6] = {2, 4, 6};
    int size = 3;
    int capacity = 6;

    int to_insert[] = {8, 10, 12, 14};
    int n = 4;

    for (int i = 0; i < n; i++) {
        if (size < capacity) {
            arr[size] = to_insert[i];
            size++;
        }
    }

    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}
A2 4 6 8 10 12 14 0
B2 4 6 8 10 12
C2 4 6 8 10 12 14
D2 4 6 8 10 12 14 14
Attempts:
2 left
💡 Hint
Check how many elements can fit given the capacity and initial size.

Practice

(1/5)
1. What happens when you insert an element at the end of a fixed-size array in C++ without resizing?
easy
A. The element replaces the last element if the array is full
B. The array automatically resizes to fit the new element
C. The element is added at the next free position if available
D. The program throws a runtime error

Solution

  1. Step 1: Understand fixed-size array behavior

    Fixed-size arrays have a set size and cannot grow automatically.
  2. Step 2: Inserting at end when full

    If the array is full, you cannot add a new element without overwriting or causing undefined behavior. If there is space available, the element is added at the next free position.
  3. Final Answer:

    The element is added at the next free position if available -> Option C
  4. Quick Check:

    Fixed-size array adds element only if space available [OK]
Hint: Fixed arrays don't resize; add element only if space available [OK]
Common Mistakes:
  • Assuming array resizes automatically
  • Thinking insertion always adds new space
  • Expecting runtime error on insertion
2. Which of the following is the correct syntax to insert an element `x` at the end of a vector `v` in C++?
easy
A. v.append(x);
B. v.push_back(x);
C. v.add(x);
D. v.insert_end(x);

Solution

  1. Step 1: Recall vector insertion method

    The standard method to add an element at the end of a vector is push_back.
  2. Step 2: Check syntax correctness

    Only v.push_back(x); is valid C++ syntax for vector insertion at end.
  3. Final Answer:

    v.push_back(x); -> Option B
  4. Quick Check:

    Use push_back to insert at vector end [OK]
Hint: Use push_back() to add at vector end [OK]
Common Mistakes:
  • Using non-existent methods like insert_end or add
  • Confusing vector with other container methods
  • Using append which is not a C++ vector method
3. What will be the output of the following C++ code?
int arr[5] = {1, 2, 3, 4, 5};
int n = 5;
int x = 10;
if (n < 5) {
  arr[n] = x;
  n++;
}
for (int i = 0; i < n; i++) {
  std::cout << arr[i] << " ";
}
medium
A. 1 2 3 4 10
B. 1 2 3 4 5 10
C. Compilation error
D. 1 2 3 4 5

Solution

  1. Step 1: Check condition for insertion

    The condition if (n < 5) is false because n equals 5, so insertion does not happen.
  2. Step 2: Print array elements

    The loop prints all elements from index 0 to 4, which are 1 2 3 4 5.
  3. Final Answer:

    1 2 3 4 5 -> Option D
  4. Quick Check:

    Insertion skipped, original array printed [OK]
Hint: Check insertion condition before adding element [OK]
Common Mistakes:
  • Assuming insertion happens even if condition false
  • Printing beyond array size
  • Confusing array size with variable n
4. Identify the error in the following code that tries to insert an element at the end of an array:
int arr[3] = {1, 2, 3};
int n = 3;
int x = 4;
arr[n] = x;
n++;
for (int i = 0; i < n; i++) {
  std::cout << arr[i] << " ";
}
medium
A. Array index out of bounds when inserting element
B. Variable n is not incremented correctly
C. Missing semicolon after arr[n] = x
D. Loop condition should be i <= n

Solution

  1. Step 1: Check array size and insertion index

    Array size is 3, valid indices are 0,1,2. Inserting at arr[3] is out of bounds.
  2. Step 2: Understand consequences of out-of-bounds access

    Accessing arr[3] causes undefined behavior or runtime error.
  3. Final Answer:

    Array index out of bounds when inserting element -> Option A
  4. Quick Check:

    Insertion index must be within array size [OK]
Hint: Array indices go from 0 to size-1 only [OK]
Common Mistakes:
  • Ignoring array bounds when inserting
  • Assuming array resizes automatically
  • Incorrect loop boundary causing extra prints
5. You have an array `int arr[6] = {1, 2, 3, 4, 5};` and a variable `int n = 5;` representing the current number of elements. Write the correct code snippet to insert `10` at the end safely, considering array size limits.
hard
A. if (n < 6) { arr[n] = 10; n++; }
B. arr[n] = 10; n++;
C. if (n <= 6) { arr[n] = 10; n++; }
D. if (n < 5) { arr[n] = 10; n++; }

Solution

  1. Step 1: Check array capacity before insertion

    Array size is 6, so insertion is safe only if n < 6.
  2. Step 2: Insert element and update count

    If condition is true, assign arr[n] = 10 and increment n to reflect new size.
  3. Final Answer:

    if (n < 6) { arr[n] = 10; n++; } -> Option A
  4. Quick Check:

    Check capacity before insert, then add element [OK]
Hint: Always check n < array size before inserting [OK]
Common Mistakes:
  • Not checking array size before insertion
  • Using <= instead of < causing overflow
  • Wrong size limit in condition
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