Bird
Raised Fist0
DSA C#programming

Array Insertion at End in DSA C#

Choose your learning style10 modes available
Mental Model
We add a new item to the last empty spot in a list of items stored in order.
Analogy: Imagine a row of boxes where each box holds one toy. To add a new toy, you put it in the first empty box at the end of the row.
Index: 0   1   2   3   4
Array: [1] [2] [3] [ ] [ ]
               ↑ insert here
Dry Run Walkthrough
Input: array: [1, 2, 3, _, _], insert value 4 at end
Goal: Add the value 4 to the first empty spot at the end of the array
Step 1: Find the first empty spot (null or default) in the array
Index: 0   1   2   3   4
Array: [1] [2] [3] [_] [_]
               ↑ insert here
Why: We need to know where to put the new value
Step 2: Place the value 4 into the empty spot at index 3
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [_]
                   ↑ next empty
Why: This adds the new value at the end
Step 3: Update the count of elements to include the new value
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [_]
                   ↑ count=4
Why: We keep track of how many items are in the array
Result:
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [_]
                   ↑ count=4
Annotated Code
DSA C#
using System;

class ArrayInsertion {
    private int[] arr;
    private int count;

    public ArrayInsertion(int size) {
        arr = new int[size];
        count = 0;
    }

    public void InsertAtEnd(int value) {
        if (count == arr.Length) {
            Console.WriteLine("Array is full, cannot insert.");
            return;
        }
        arr[count] = value; // place value at first empty spot
        count++; // increase count to include new value
    }

    public void PrintArray() {
        for (int i = 0; i < count; i++) {
            Console.Write(arr[i]);
            if (i < count - 1) Console.Write(" -> ");
        }
        Console.WriteLine(" -> null");
    }

    static void Main() {
        ArrayInsertion array = new ArrayInsertion(5);
        array.InsertAtEnd(1);
        array.InsertAtEnd(2);
        array.InsertAtEnd(3);
        array.InsertAtEnd(4);
        array.PrintArray();
    }
}
if (count == arr.Length) {
check if array is full to avoid overflow
arr[count] = value; // place value at first empty spot
insert new value at the current count index
count++; // increase count to include new value
update count to reflect new element
OutputSuccess
1 -> 2 -> 3 -> 4 -> null
Complexity Analysis
Time: O(1) because insertion at the end uses the count index directly without searching
Space: O(n) because the array size is fixed and stores n elements
vs Alternative: Compared to shifting elements in an array for insertion at start or middle (O(n)), insertion at end is faster and simpler
Edge Cases
inserting into a full array
insertion is rejected with a message to avoid overflow
DSA C#
if (count == arr.Length) {
inserting into an empty array
value is placed at index 0 and count becomes 1
DSA C#
arr[count] = value;
When to Use This Pattern
When you need to add items to a fixed-size list quickly and in order, use array insertion at end to avoid costly shifts.
Common Mistakes
Mistake: Trying to insert without checking if the array is full causes errors.
Fix: Always check if count equals array length before inserting.
Mistake: Not updating the count after insertion leads to overwriting values.
Fix: Increment count after placing the new value.
Summary
Adds a new value to the first empty spot at the end of an array.
Use when you have a fixed-size array and want to add items sequentially.
Keep track of the count to know where to insert next and avoid overflow.

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