Array Insertion at End in DSA C# - Time & Space 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?
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 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.
Each insertion takes the same small number of steps, no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even if the array grows larger.
Time Complexity: O(1)
This means adding an item at the end takes a fixed amount of time regardless of array size.
[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.
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.
"What if the array is full and we need to create a bigger array and copy items? How would the time complexity change?"
Practice
Solution
Step 1: Understand fixed-size array behavior
In C#, arrays have a fixed size once created and cannot grow automatically.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.Final Answer:
An error occurs because the array size is fixed -> Option AQuick Check:
Fixed-size array insert at end causes error [OK]
- Assuming array resizes automatically
- Thinking insertion shifts elements
- Believing insertion adds new slot
numbers in C#?Solution
Step 1: Identify List insertion method
In C#, List<T> uses the Add() method to insert elements at the end.Step 2: Check other options
InsertEnd, Push, and Append are not valid List<T> methods in C#.Final Answer:
numbers.Add(5); -> Option CQuick Check:
List Add method inserts at end [OK]
- Using non-existent methods like InsertEnd or Push
- Confusing List with Stack methods
- Trying Append which is for strings
int[] arr = new int[3] {1, 2, 3};
arr[2] = 10;
Console.WriteLine(string.Join(", ", arr));Solution
Step 1: Understand array initialization
The arrayarrhas 3 elements: 1, 2, 3 at indexes 0, 1, 2.Step 2: Assign new value at last index
arr[2] = 10 replaces the last element (3) with 10.Final Answer:
1, 2, 10 -> Option BQuick Check:
Last element replaced with 10 [OK]
- Thinking array resizes or shifts elements
- Expecting original array unchanged
- Assuming runtime error on assignment
int[] arr = new int[3] {1, 2, 3};
arr[arr.Length] = 7;Solution
Step 1: Check array index used
arr.Length is 3, but valid indexes are 0,1,2 only.Step 2: Understand index out of range
Accessing arr[3] causes an IndexOutOfRangeException at runtime.Final Answer:
Index out of range error -> Option DQuick Check:
Array index must be less than length [OK]
- Using arr.Length as index directly
- Ignoring runtime exceptions
- Confusing syntax errors with runtime errors
int[] arr = new int[3] {1, 2, 3};. You want to insert 4 at the end without losing existing elements. Which approach is correct?Solution
Step 1: Understand fixed array size
Arrays in C# have fixed size; cannot add elements beyond initial size.Step 2: Correct way to add element
Create a new larger array, copy existing elements, then insert new element at the end.Step 3: Check other options
Assigning arr[3] causes error; arr.Add() doesn't exist for arrays; arr.Resize() is invalid.Final Answer:
Create a new array of size 4, copy old elements, then add 4 at last index -> Option AQuick Check:
Fixed arrays need new array to grow [OK]
- Trying to assign beyond array length
- Using List methods on arrays
- Assuming Resize method exists
Ready to prove it?
Pass the test and this pattern moves unknown
