Array Insertion at Beginning in DSA C# - Time & Space Complexity
We want to understand how long it takes to add a new item at the start of an array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
int[] InsertAtBeginning(int[] arr, int value) {
int n = arr.Length;
int[] newArr = new int[n + 1];
newArr[0] = value;
for (int i = 0; i < n; i++) {
newArr[i + 1] = arr[i];
}
return newArr;
}
This code creates a new array and copies all elements from the old array, shifting them right to insert the new value at the start.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop copying each element from the old array to the new array.
- How many times: Exactly once for each element in the original array (n times).
As the array size grows, the number of copy steps grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 copy steps |
| 100 | About 100 copy steps |
| 1000 | About 1000 copy steps |
Pattern observation: The work grows directly with the number of items; doubling the array size roughly doubles the work.
Time Complexity: O(n)
This means the time to insert at the beginning grows linearly with the array size.
[X] Wrong: "Inserting at the start of an array is always fast and constant time like adding at the end."
[OK] Correct: Because arrays need to move all existing items to make space at the start, so the time grows with the array size.
Knowing this helps you explain why arrays are not ideal for frequent insertions at the start, and shows you understand how data structure choices affect performance.
"What if we used a linked list instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand fixed-size array behavior
In C#, arrays have fixed size, so to insert at the beginning, elements must shift right.Step 2: Effect of insertion at index 0
The element at index 0 is replaced by the new element, and all others move one index higher.Final Answer:
All existing elements shift one position to the right to make space. -> Option DQuick Check:
Insert at start = shift right [OK]
- Thinking array size grows automatically
- Replacing first element without shifting
- Removing last element silently
arr of size 5, shifting elements right?Solution
Step 1: Identify correct loop for shifting right
Loop must start from the end and move elements one step right to avoid overwriting.Step 2: Assign new value at index 0
After shifting, assign 5 to arr[0].Final Answer:
for (int i = arr.Length - 1; i > 0; i--) { arr[i] = arr[i - 1]; } arr[0] = 5; -> Option AQuick Check:
Shift right from end, then insert [OK]
- Shifting left instead of right
- Assigning without shifting
- Using invalid index arr.Length
int[] arr = {10, 20, 30, 40, 50};
for (int i = arr.Length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = 5;
foreach (int num in arr) {
Console.Write(num + " ");
}Solution
Step 1: Shift elements right
Elements move right: arr[4]=arr[3]=40, arr[3]=arr[2]=30, arr[2]=arr[1]=20, arr[1]=arr[0]=10Step 2: Insert 5 at index 0
arr[0] = 5Final Answer:
5 10 20 30 40 -> Option BQuick Check:
Shift right + insert 5 at start = 5 10 20 30 40 [OK]
- Not shifting elements correctly
- Overwriting elements before shifting
- Incorrect final array print
arr but produces incorrect output. What is the error?int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.Length - 1; i++) {
arr[i + 1] = arr[i];
}
arr[0] = 99;
Solution
Step 1: Analyze loop direction and range
The loop starts from 0 and moves forward, causing overwriting of elements.Step 2: Correct loop should start from end
To shift right safely, loop must start from last index and move backward.Final Answer:
The loop index range is incorrect; it should start from the end. -> Option CQuick Check:
Shift right requires backward loop [OK]
- Looping forward overwrites elements
- Assigning new value inside loop
- Ignoring fixed array size
arr = new int[6] {0, 1, 2, 3, 4, 5} and want to insert a new element 99 at the beginning without losing any data. Which approach correctly handles this?Solution
Step 1: Understand fixed array size limitation
Original array size is 6, so inserting without losing data requires a bigger array.Step 2: Create new larger array and copy elements
Create new array size 7, assign 99 at index 0, then copy original elements starting at index 1.Final Answer:
Create a new array of size 7, set arr2[0] = 99, then copy all elements of arr to arr2 starting at index 1. -> Option AQuick Check:
New bigger array + copy elements = safe insertion [OK]
- Trying to insert in fixed-size array losing last element
- Replacing without shifting
- Ignoring better data structures like List
Ready to prove it?
Pass the test and this pattern moves unknown
