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
What is the output after inserting an element at the end of the array?
Consider the following C# code that inserts an element at the end of an array by creating a new array with increased size. What will be printed after insertion?
DSA C#
int[] InsertAtEnd(int[] arr, int value) {
    int[] newArr = new int[arr.Length + 1];
    for (int i = 0; i < arr.Length; i++) {
        newArr[i] = arr[i];
    }
    newArr[newArr.Length - 1] = value;
    return newArr;
}

int[] original = {1, 2, 3};
int[] result = InsertAtEnd(original, 4);
for (int i = 0; i < result.Length; i++) {
    Console.Write(result[i] + " -> ");
}
Console.WriteLine("null");
A4 -> 1 -> 2 -> 3 -> null
B1 -> 2 -> 3 -> null
C1 -> 2 -> 3 -> 0 -> null
D1 -> 2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Think about how the new array copies old elements and adds the new value at the last position.
❓ Predict Output
intermediate
2:00remaining
What error occurs when inserting at end without resizing the array?
What happens if you try to insert an element at the end of a fixed-size array in C# without creating a new array?
DSA C#
int[] arr = {1, 2, 3};
arr[arr.Length] = 4;
Console.WriteLine(string.Join(", ", arr));
ANo error, output: 1, 2, 3, 4
BIndexOutOfRangeException
CNullReferenceException
DCompilation error
Attempts:
2 left
💡 Hint
Arrays have fixed size; accessing index equal to length is invalid.
🔧 Debug
advanced
3:00remaining
Find the bug in this array insertion code
This code tries to insert a value at the end of an array but produces wrong output. What is the bug?
DSA C#
int[] InsertAtEnd(int[] arr, int value) {
    int[] newArr = new int[arr.Length];
    for (int i = 0; i < arr.Length; i++) {
        newArr[i] = arr[i];
    }
    newArr[newArr.Length - 1] = value;
    return newArr;
}

int[] original = {1, 2, 3};
int[] result = InsertAtEnd(original, 4);
for (int i = 0; i < result.Length; i++) {
    Console.Write(result[i] + " -> ");
}
Console.WriteLine("null");
AThe value is inserted at the start instead of the end
BThe loop copies elements incorrectly causing IndexOutOfRangeException
CThe new array size is not increased, so last element overwrites existing data
DThe function returns the original array instead of new array
Attempts:
2 left
💡 Hint
Check the size of the new array compared to the original.
🧠 Conceptual
advanced
2:30remaining
Why is inserting at the end of an array costly?
Inserting an element at the end of a fixed-size array requires creating a new array and copying elements. Why is this operation costly compared to linked lists?
ABecause arrays require copying all elements to a new larger array, which takes time proportional to array size
BBecause arrays do not allow storing integers
CBecause linked lists use more memory than arrays
DBecause arrays automatically resize without copying
Attempts:
2 left
💡 Hint
Think about what happens internally when you add an element beyond the array's fixed size.
❓ Predict Output
expert
3:00remaining
What is the output after multiple insertions at the end?
Given the code below that inserts multiple elements at the end of an array, what is the final printed output?
DSA C#
int[] InsertAtEnd(int[] arr, int value) {
    int[] newArr = new int[arr.Length + 1];
    for (int i = 0; i < arr.Length; i++) {
        newArr[i] = arr[i];
    }
    newArr[newArr.Length - 1] = value;
    return newArr;
}

int[] arr = {1};
arr = InsertAtEnd(arr, 2);
arr = InsertAtEnd(arr, 3);
arr = InsertAtEnd(arr, 4);
for (int i = 0; i < arr.Length; i++) {
    Console.Write(arr[i] + " -> ");
}
Console.WriteLine("null");
A1 -> 2 -> 3 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
C1 -> 2 -> 3 -> null
D4 -> 3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Each insertion creates a new array with one more element, preserving previous elements in order.

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. An error occurs because the array size is fixed
B. The array automatically resizes to fit the new element
C. The element replaces the last element if the array is full
D. The element is added at the first position

Solution

  1. Step 1: Understand fixed-size array behavior

    In C#, arrays have a fixed size once created and cannot grow automatically.
  2. Step 2: Inserting at the end without resizing

    Inserting at the end requires accessing index arr.Length, which is out of bounds and throws IndexOutOfRangeException.
  3. Final Answer:

    An error occurs because the array size is fixed -> Option A
  4. Quick Check:

    Fixed-size array insert at end causes error [OK]
Hint: Fixed arrays can't grow; insert at end causes error [OK]
Common Mistakes:
  • Assuming array resizes automatically
  • Thinking insertion shifts elements
  • Believing insertion adds new slot
2. Which of the following is the correct syntax to insert an element at the end of a List<int> named numbers in C#?
easy
A. numbers.Push(5);
B. numbers.InsertEnd(5);
C. numbers.Add(5);
D. numbers.Append(5);

Solution

  1. Step 1: Identify List insertion method

    In C#, List<T> uses the Add() method to insert elements at the end.
  2. Step 2: Check other options

    InsertEnd, Push, and Append are not valid List<T> methods in C#.
  3. Final Answer:

    numbers.Add(5); -> Option C
  4. Quick Check:

    List Add method inserts at end [OK]
Hint: Use Add() to insert at end of List [OK]
Common Mistakes:
  • Using non-existent methods like InsertEnd or Push
  • Confusing List with Stack methods
  • Trying Append which is for strings
3. What will be the output of the following C# code?
int[] arr = new int[3] {1, 2, 3};
arr[2] = 10;
Console.WriteLine(string.Join(", ", arr));
medium
A. 1, 2, 3
B. 1, 2, 10
C. 10, 2, 3
D. Runtime error

Solution

  1. Step 1: Understand array initialization

    The array arr has 3 elements: 1, 2, 3 at indexes 0, 1, 2.
  2. Step 2: Assign new value at last index

    arr[2] = 10 replaces the last element (3) with 10.
  3. Final Answer:

    1, 2, 10 -> Option B
  4. Quick Check:

    Last element replaced with 10 [OK]
Hint: Index 2 is last element; assignment replaces it [OK]
Common Mistakes:
  • Thinking array resizes or shifts elements
  • Expecting original array unchanged
  • Assuming runtime error on assignment
4. Identify the error in the following code that tries to insert 7 at the end of an array:
int[] arr = new int[3] {1, 2, 3};
arr[arr.Length] = 7;
medium
A. Syntax error due to missing semicolon
B. Null reference exception
C. No error, code runs fine
D. Index out of range error

Solution

  1. Step 1: Check array index used

    arr.Length is 3, but valid indexes are 0,1,2 only.
  2. Step 2: Understand index out of range

    Accessing arr[3] causes an IndexOutOfRangeException at runtime.
  3. Final Answer:

    Index out of range error -> Option D
  4. Quick Check:

    Array index must be less than length [OK]
Hint: Array indexes go 0 to Length-1 only [OK]
Common Mistakes:
  • Using arr.Length as index directly
  • Ignoring runtime exceptions
  • Confusing syntax errors with runtime errors
5. You have an array int[] arr = new int[3] {1, 2, 3};. You want to insert 4 at the end without losing existing elements. Which approach is correct?
hard
A. Create a new array of size 4, copy old elements, then add 4 at last index
B. Assign arr[3] = 4 directly
C. Use arr.Add(4) method
D. Resize arr using arr.Resize(4) method

Solution

  1. Step 1: Understand fixed array size

    Arrays in C# have fixed size; cannot add elements beyond initial size.
  2. Step 2: Correct way to add element

    Create a new larger array, copy existing elements, then insert new element at the end.
  3. Step 3: Check other options

    Assigning arr[3] causes error; arr.Add() doesn't exist for arrays; arr.Resize() is invalid.
  4. Final Answer:

    Create a new array of size 4, copy old elements, then add 4 at last index -> Option A
  5. Quick Check:

    Fixed arrays need new array to grow [OK]
Hint: Arrays fixed size; create new bigger array to add [OK]
Common Mistakes:
  • Trying to assign beyond array length
  • Using List methods on arrays
  • Assuming Resize method exists
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