Array Insertion at End in DSA Java - Time & Space Complexity
We want to understand how long it takes to add a new item at 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.
public class ArrayInsertion {
public static int insertAtEnd(int[] arr, int size, int value) {
arr[size] = value; // Insert value at the end
return size + 1; // Return new size
}
}
This code adds a new value at the end of an array and returns the updated size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single direct assignment to the array at the last index.
- How many times: Exactly once per insertion.
Adding one item at the end always takes the same amount of work, no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same even as the array grows larger.
Time Complexity: O(1)
This means adding an item at the end takes a constant amount of time regardless of array size.
[X] Wrong: "Adding at the end takes longer as the array grows because we have to move all elements."
[OK] Correct: We do not move existing elements when adding at the end; we just place the new item directly at the next free spot.
Knowing that adding at the end of an array is quick helps you understand when arrays are a good choice for storing data.
"What if the array is full and we need to create a bigger array to insert the new item? How would the time complexity change?"
Practice
Solution
Step 1: Understand fixed-size array behavior
Java arrays have a fixed size and cannot grow automatically. Attempting arr[size] where size == length throws ArrayIndexOutOfBoundsException.Step 2: Inserting at end when full
Standard insertion arr[size++] = element; if full (size == arr.length), accesses arr[length] and throws runtime error.Final Answer:
The insertion throws an error if the array is full -> Option AQuick Check:
Fixed-size array throws error when full [OK]
- Assuming arrays resize automatically
- Thinking insertion overwrites last element
- Believing new element is added beyond array size
Solution
Step 1: Understand array indexing and size tracking
Arrays use zero-based indexing; last index is length-1. We track current size separately.Step 2: Insert element at next free position
Use size as index to insert, then increment size to reflect new element count.Final Answer:
arr[size] = newElement; size++; -> Option AQuick Check:
Use size as index and increment after insertion [OK]
- Using arr.length as index (out of bounds)
- Trying to use arr.add() which is for lists
- Overwriting last element without size tracking
int[] arr = new int[5]; int size = 0; arr[size++] = 10; arr[size++] = 20; arr[size++] = 30; System.out.print(java.util.Arrays.toString(arr));
Solution
Step 1: Trace insertions with size increment
Elements 10, 20, 30 are inserted at indexes 0, 1, 2 respectively; size increments after each insertion.Step 2: Understand default array values
Remaining positions (indexes 3 and 4) remain 0 as default for int arrays.Final Answer:
[10, 20, 30, 0, 0] -> Option BQuick Check:
Inserted elements at start; rest are zeros [OK]
- Assuming array shrinks to inserted elements only
- Confusing index positions
- Expecting uninitialized elements to be null
int[] arr = new int[3]; int size = 3; arr[size] = 5;
Solution
Step 1: Check array size and index used
Array length is 3, valid indexes are 0,1,2. Using index 3 causes error.Step 2: Understand runtime exception
Accessing arr[3] throws ArrayIndexOutOfBoundsException at runtime.Final Answer:
ArrayIndexOutOfBoundsException because size equals array length -> Option CQuick Check:
Index equals length causes runtime error [OK]
- Assuming index == length is valid
- Expecting compile-time error instead of runtime
- Ignoring size variable correctness
int[] arr = {1, 2, 3, 4}; and a variable size = 4;. You want to insert 5 at the end but the array is full. Which approach correctly adds 5 at the end without losing existing data?Solution
Step 1: Recognize fixed array size limitation
Original array size is 4; cannot add element beyond index 3 without resizing.Step 2: Create new larger array and copy elements
Create new array of size 5, copy existing elements, then insert 5 at index 4.Final Answer:
Create a new array of size 5, copy old elements, then add 5 at index 4 -> Option DQuick Check:
Resize by new array and copy before insertion [OK]
- Trying to insert without resizing
- Overwriting last element losing data
- Assuming size variable controls array length
Ready to prove it?
Pass the test and this pattern moves unknown
