Bird
Raised Fist0
DSA Cprogramming~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
What is the output after inserting an element at the end?
Consider the following C code that inserts an element at the end of an array. What will be printed after insertion?
DSA C
int main() {
    int arr[5] = {1, 2, 3, 4};
    int size = 4;
    int capacity = 5;
    int element = 10;

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

    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A1 2 3 4
B1 2 3 4 0
C10 1 2 3 4
D1 2 3 4 10
Attempts:
2 left
💡 Hint
Think about how the element is added and how the size changes.
❓ Predict Output
intermediate
2:00remaining
What happens if we insert beyond array capacity?
What will be the output of this C code snippet?
DSA C
int main() {
    int arr[3] = {5, 6, 7};
    int size = 3;
    int capacity = 3;
    int element = 9;

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

    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A5 6 7
B5 6 7 9
C9 5 6 7
D5 6 7 0
Attempts:
2 left
💡 Hint
Check the condition before insertion carefully.
🔧 Debug
advanced
2:00remaining
Why does this insertion code cause unexpected output?
Identify the problem in this code that inserts an element at the end of an array and prints it.
DSA C
int main() {
    int arr[4] = {1, 2, 3};
    int size = 3;
    int capacity = 4;
    int element = 8;

    arr[size] = element;
    // Missing size increment here

    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
AOutput: 1 2 3
BOutput: 1 2 3 8
COutput: 1 2 3 0
DRuntime error due to out of bounds
Attempts:
2 left
💡 Hint
Check if the size variable is updated after insertion.
❓ Predict Output
advanced
2:00remaining
What is the output after multiple insertions at the end?
Given the following code, what will be printed?
DSA C
int main() {
    int arr[6] = {2, 4, 6};
    int size = 3;
    int capacity = 6;
    int elements_to_add[3] = {8, 10, 12};

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

    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A2 4 6 8 10
B2 4 6 8 10 12
C2 4 6 0 0 0
D8 10 12 2 4 6
Attempts:
2 left
💡 Hint
Look at how the loop adds elements and updates size.
🧠 Conceptual
expert
2:00remaining
What is the main limitation of inserting at the end of a static array?
Why can inserting an element at the end of a static array fail or be limited?
ABecause the array automatically resizes causing slow insertion
BBecause elements must be shifted to insert at the end
CBecause the array size is fixed and cannot grow beyond its capacity
DBecause inserting at the end requires sorting the array
Attempts:
2 left
💡 Hint
Think about the difference between static and dynamic arrays.

Practice

(1/5)
1. What happens when you insert an element at the end of an array in C if the array has enough space?
easy
A. The element is added at the last position and the array size increases by one.
B. The element replaces the first element of the array.
C. The array size remains the same and the element is ignored.
D. The array is sorted automatically after insertion.

Solution

  1. Step 1: Understand array insertion at end

    When an element is inserted at the end, it is placed at the next free index if space is available.
  2. Step 2: Effect on array size

    The logical size of the array increases by one because a new element is added.
  3. Final Answer:

    The element is added at the last position and the array size increases by one. -> Option A
  4. Quick Check:

    Insert at end adds element and increases size [OK]
Hint: Insert at end adds element at next free index [OK]
Common Mistakes:
  • Thinking array size changes physically (fixed size in C)
  • Assuming insertion replaces existing elements
  • Believing array sorts automatically after insertion
2. Which of the following is the correct syntax to insert an element 'x' at the end of an array 'arr' with current size 'n' in C?
easy
A. arr[n+1] = x; n++;
B. arr[n] = x; n++;
C. arr[n-1] = x; n++;
D. arr[x] = n; n++;

Solution

  1. Step 1: Identify correct index for insertion

    The next free position is at index 'n' since array indexing starts at 0 and current size is 'n'.
  2. Step 2: Update size after insertion

    After inserting, increment 'n' to reflect the new size.
  3. Final Answer:

    arr[n] = x; n++; -> Option B
  4. Quick Check:

    Insert at arr[n], then increment n [OK]
Hint: Insert at arr[n], then increase size n [OK]
Common Mistakes:
  • Using arr[n+1] causes out-of-bounds error
  • Using arr[n-1] overwrites last element
  • Confusing element and index positions
3. What will be the output of the following C code?
int arr[5] = {1, 2, 3};
int n = 3;
arr[n] = 4;
n++;
for(int i = 0; i < n; i++) {
    printf("%d ", arr[i]);
}
medium
A. 1 2 3 4
B. 1 2 3
C. 1 2 3 0
D. Compilation error

Solution

  1. Step 1: Insert element at arr[3]

    arr[3] is assigned 4, so array becomes {1, 2, 3, 4, ?}.
  2. Step 2: Increment size and print elements

    n becomes 4, loop prints arr[0] to arr[3]: 1 2 3 4.
  3. Final Answer:

    1 2 3 4 -> Option A
  4. Quick Check:

    Inserted 4 at end, printed all elements [OK]
Hint: Insert at arr[n], increment n, print up to n [OK]
Common Mistakes:
  • Ignoring increment of n, printing only first 3 elements
  • Assuming uninitialized arr[3] is zero
  • Confusing array size and capacity
4. Find the error in the following code snippet that inserts an element at the end of an array:
int arr[5] = {10, 20, 30};
int n = 3;
arr[n+1] = 40;
n++;
medium
A. Array initialization is incorrect.
B. The size 'n' is not incremented correctly.
C. No error, code works fine.
D. The element is inserted at wrong index (arr[n+1] instead of arr[n]).

Solution

  1. Step 1: Check insertion index

    Inserting at arr[n+1] means arr[4], skipping arr[3], which is incorrect for sequential insertion.
  2. Step 2: Consequence of wrong index

    This causes a gap in the array; correct index is arr[n].
  3. Final Answer:

    The element is inserted at wrong index (arr[n+1] instead of arr[n]). -> Option D
  4. Quick Check:

    Insert at arr[n], not arr[n+1] [OK]
Hint: Insert at arr[n], not arr[n+1] to avoid errors [OK]
Common Mistakes:
  • Using arr[n+1] instead of arr[n]
  • Not updating size after insertion
  • Ignoring array bounds
5. Given an array int arr[6] = {5, 10, 15, 20}; with current size n = 4, which code snippet correctly inserts the element 25 at the end and prints the updated array?
medium
A. arr[n-1] = 25; n++; for(int i=0; i
B. arr[n+1] = 25; n++; for(int i=0; i
C. arr[n] = 25; n++; for(int i=0; i
D. arr[5] = 25; n = 5; for(int i=0; i

Solution

  1. Step 1: Identify correct insertion index

    Current size n=4 (indices 0-3 used), so end insertion at arr[4] (arr[n]), then n=5.
  2. Step 2: Evaluate options

    A: arr[4]=25, n=5, prints 5 10 15 20 25 [correct].
    B: arr[5]=25, n=5, prints 0-4 (5 10 15 20 <uninit>) [misses 25].
    C: arr[3]=25 overwrites 20 [wrong].
    D: arr[5]=25, n=5, prints 0-4 [misses 25].
  3. Final Answer:

    arr[n] = 25; n++; for(int i=0; i -> Option C
  4. Quick Check:

    Insert at arr[n], n++, print 0 to n-1 [OK]
Hint: Insert at next free index, update size, print all elements [OK]
Common Mistakes:
  • Inserting at wrong index causing overwrite
  • Not updating size correctly
  • Printing wrong number of elements
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