Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting 99 at the middle index?
Given the array
int[] arr = {10, 20, 30, 40, 50};, an insertion of 99 is done at the middle index. What is the resulting array?DSA Java
int[] arr = {10, 20, 30, 40, 50}; int middle = arr.length / 2; int[] newArr = new int[arr.length + 1]; for (int i = 0, j = 0; i < newArr.length; i++) { if (i == middle) { newArr[i] = 99; } else { newArr[i] = arr[j++]; } } System.out.println(java.util.Arrays.toString(newArr));
Attempts:
2 left
💡 Hint
Remember that the middle index for an array of length 5 is 2 (integer division).
✗ Incorrect
The middle index is calculated as length/2 = 5/2 = 2. The new element 99 is inserted at index 2, shifting the original elements from index 2 onwards to the right.
❓ Predict Output
intermediateWhat is the output after inserting 77 at the middle index of an even-length array?
Given the array
int[] arr = {5, 15, 25, 35};, an insertion of 77 is done at the middle index. What is the resulting array?DSA Java
int[] arr = {5, 15, 25, 35}; int middle = arr.length / 2; int[] newArr = new int[arr.length + 1]; for (int i = 0, j = 0; i < newArr.length; i++) { if (i == middle) { newArr[i] = 77; } else { newArr[i] = arr[j++]; } } System.out.println(java.util.Arrays.toString(newArr));
Attempts:
2 left
💡 Hint
For even length arrays, middle index is length/2, which is 2 here.
✗ Incorrect
The middle index is 4/2 = 2. The new element 77 is inserted at index 2, shifting elements from index 2 onwards to the right.
🔧 Debug
advancedIdentify the error in this array insertion code
What error will this code produce when inserting 50 at the middle index of
int[] arr = {1, 2, 3, 4, 5};?DSA Java
int[] arr = {1, 2, 3, 4, 5}; int middle = arr.length / 2; int[] newArr = new int[arr.length + 1]; for (int i = 0; i < newArr.length; i++) { if (i == middle) { newArr[i] = 50; } else { newArr[i] = arr[i]; } } System.out.println(java.util.Arrays.toString(newArr));
Attempts:
2 left
💡 Hint
Check how the original array is accessed inside the loop when i > middle.
✗ Incorrect
The code tries to access arr[i] for i from 0 to newArr.length-1 (which is 6), but arr has length 5. When i reaches 5, arr[5] is out of bounds causing ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedWhat is the output after inserting 0 at the middle index of an empty array?
Given an empty array
int[] arr = {};, an insertion of 0 is done at the middle index. What is the resulting array?DSA Java
int[] arr = {}; int middle = arr.length / 2; int[] newArr = new int[arr.length + 1]; for (int i = 0, j = 0; i < newArr.length; i++) { if (i == middle) { newArr[i] = 0; } else { newArr[i] = arr[j++]; } } System.out.println(java.util.Arrays.toString(newArr));
Attempts:
2 left
💡 Hint
An empty array has length 0, so middle index is 0.
✗ Incorrect
Since the original array is empty, the new array has length 1. The element 0 is inserted at index 0, resulting in [0].
🧠 Conceptual
expertHow many elements are in the array after inserting one element at the middle index?
If you start with an array of length
n and insert one element at the middle index, how many elements will the new array contain?Attempts:
1 left
💡 Hint
Inserting means adding one new element, so the size changes accordingly.
✗ Incorrect
Inserting one element increases the array size by one, so the new array length is n + 1.
