Array Insertion at Beginning in DSA Java - 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.
public void insertAtBeginning(int[] arr, int n, int value) {
for (int i = n - 1; i >= 0; i--) {
arr[i + 1] = arr[i];
}
arr[0] = value;
}
This code shifts all elements one step right to make space, then inserts the new value at the start.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves each element one position to the right.
- How many times: It runs once for each element already in the array, so n times.
As the array size grows, the number of moves grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 moves |
| 100 | 100 moves |
| 1000 | 1000 moves |
Pattern observation: The work grows directly with the number of elements.
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 is always fast because it's just one step."
[OK] Correct: Because all existing elements must move one step right, the work depends on how many elements are there.
Knowing this helps you explain why arrays are not always the best choice for adding items at the start, showing your understanding of data structure trade-offs.
"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
Java arrays have fixed size, so to insert at the start, elements must move to make space.Step 2: Describe element shifting
All elements shift one position right to free index 0 for the new element.Final Answer:
All existing elements shift one position to the right to make space. -> Option CQuick 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?Solution
Step 1: Identify correct shifting direction
To insert at start, shift elements right from end to start to avoid overwriting.Step 2: Check code correctness
for(int i = arr.length - 1; i > 0; i--) { arr[i] = arr[i-1]; } arr[0] = 10; shifts elements right correctly, then inserts 10 at index 0.Final Answer:
for(int i = arr.length - 1; i > 0; i--) { arr[i] = arr[i-1]; } arr[0] = 10; -> Option DQuick Check:
Shift right from end, then insert [OK]
- Shifting left instead of right
- Overwriting elements before shifting
- Trying to insert at arr[arr.length] (out of bounds)
int[] arr = {5, 6, 7, 8, 9};
for(int i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i-1];
}
arr[0] = 1;
for(int num : arr) {
System.out.print(num + " ");
}Solution
Step 1: Trace the shifting loop
Loop shifts elements right: arr[4]=arr[3]=8, arr[3]=arr[2]=7, arr[2]=arr[1]=6, arr[1]=arr[0]=5.Step 2: Insert 1 at index 0
arr[0] = 1 replaces original 5.Final Answer:
1 5 6 7 8 -> Option BQuick Check:
Shift right + insert 1 at start = 1 5 6 7 8 [OK]
- Not shifting elements before insertion
- Overwriting elements incorrectly
- Confusing output order
arr.int[] arr = {2, 4, 6, 8, 10};
for(int i = 0; i < arr.length - 1; i++) {
arr[i+1] = arr[i];
}
arr[0] = 20;Solution
Step 1: Analyze loop direction
The loop shifts elements from start to end, causing overwriting of values before they move.Step 2: Correct loop direction
To avoid overwriting, loop must run backward from end to start.Final Answer:
The loop should start from the end to avoid overwriting. -> Option AQuick Check:
Shift right requires backward loop [OK]
- Looping forward causes data overwrite
- Ignoring array size limits
- Assuming code works without shifting properly
arr of length 6 with elements {3, 5, 7, 9, 11, 13}, you want to insert 1 at the beginning without losing any data. Which approach correctly handles this?Solution
Step 1: Understand fixed-size array limits
Java arrays cannot grow; to keep all data, a bigger array is needed.Step 2: Copy elements to new array shifted by one
Create new array size 7, copy old elements starting at index 1, insert 1 at index 0.Final Answer:
Create a new array of size 7, copy all elements shifted by one, then insert 1 at index 0. -> Option AQuick Check:
New bigger array + shift + insert = no data loss [OK]
- Overwriting last element in fixed array
- Ignoring array size limits
- Trying to insert out of bounds
Ready to prove it?
Pass the test and this pattern moves unknown
