Bird
Raised Fist0
DSA C#programming~5 mins

Array Insertion at End in DSA C# - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Array Insertion at End
O(1)
Understanding Time Complexity

We want to understand how long it takes to add an item to the end of an array.

How does the time needed change as the array grows bigger?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


int[] arr = new int[10];
int count = 0;

void InsertAtEnd(int value) {
    if (count < arr.Length) {
        arr[count] = value;
        count++;
    }
}
    

This code adds a new value at the end of the array if there is space.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Assigning a value to the array at the current end index.
  • How many times: Once per insertion call.
How Execution Grows With Input

Each insertion takes the same small number of steps, no matter how big the array is.

Input Size (n)Approx. Operations
101
1001
10001

Pattern observation: The time stays the same even if the array grows larger.

Final Time Complexity

Time Complexity: O(1)

This means adding an item at the end takes a fixed amount of time regardless of array size.

Common Mistake

[X] Wrong: "Adding at the end takes longer as the array gets bigger because we have to move all items."

[OK] Correct: We do not move existing items; we just place the new item at the next free spot, so time stays constant.

Interview Connect

Knowing that adding at the end of an array is quick helps you explain why arrays are good for fast appends when space is available.

Self-Check

"What if the array is full and we need to create a bigger array and copy items? How would the time complexity change?"

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