Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting element at the beginning of an array
What is the output of the following Java code that inserts an element at the beginning of an array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {2, 3, 4}; int newElement = 1; int[] newArr = new int[arr.length + 1]; newArr[0] = newElement; for (int i = 0; i < arr.length; i++) { newArr[i + 1] = arr[i]; } for (int num : newArr) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Think about how the new element is placed at index 0 and the rest are shifted.
✗ Incorrect
The new element 1 is inserted at the start (index 0), and the original elements are shifted right by one position.
🧠 Conceptual
intermediateUnderstanding array insertion at beginning complexity
What is the time complexity of inserting an element at the beginning of a fixed-size array in Java by creating a new array and copying elements?
Attempts:
2 left
💡 Hint
Consider how many elements need to be copied to the new array.
✗ Incorrect
Since all elements must be copied to the new array shifted by one position, the operation takes linear time proportional to the array size.
🔧 Debug
advancedIdentify the error in array insertion at beginning code
What error will the following Java code produce when trying to insert an element at the beginning of an array?
DSA Java
int[] arr = {5, 6, 7}; int newElement = 4; for (int i = arr.length - 1; i >= 0; i--) { arr[i + 1] = arr[i]; } arr[0] = newElement; for (int num : arr) { System.out.print(num + " "); }
Attempts:
2 left
💡 Hint
Think about the size of the original array and the index accessed in the loop.
✗ Incorrect
The code tries to access arr[i + 1] where i + 1 equals arr.length, which is outside the array bounds causing ArrayIndexOutOfBoundsException.
🚀 Application
advancedResult of multiple insertions at beginning
Given an empty array and the following insertions at the beginning, what is the final array?
DSA Java
int[] arr = new int[0]; int[] temp; int[] insertAtBeginning(int[] array, int element) { temp = new int[array.length + 1]; temp[0] = element; for (int i = 0; i < array.length; i++) { temp[i + 1] = array[i]; } return temp; } arr = insertAtBeginning(arr, 3); arr = insertAtBeginning(arr, 2); arr = insertAtBeginning(arr, 1); for (int num : arr) { System.out.print(num + " "); }
Attempts:
2 left
💡 Hint
Each insertion adds the new element at the start, pushing previous elements right.
✗ Incorrect
First insert 3: [3], then insert 2 at start: [2,3], then insert 1 at start: [1,2,3].
🧠 Conceptual
expertWhy arrays are inefficient for frequent insertions at beginning
Why is inserting elements at the beginning of a fixed-size array inefficient compared to other data structures?
Attempts:
2 left
💡 Hint
Think about what happens to existing elements when you insert at the start.
✗ Incorrect
Arrays have fixed size and contiguous memory, so inserting at the start requires moving all elements one position to the right, which is costly.
