Bird
Raised Fist0
DSA Javaprogramming~20 mins

Array Insertion at Middle Index in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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));
A[10, 20, 99, 30, 40, 50]
B[10, 20, 30, 99, 40, 50]
C[10, 20, 30, 40, 99, 50]
D[99, 10, 20, 30, 40, 50]
Attempts:
2 left
💡 Hint
Remember that the middle index for an array of length 5 is 2 (integer division).
❓ Predict Output
intermediate
2:00remaining
What 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));
A[77, 5, 15, 25, 35]
B[5, 77, 15, 25, 35]
C[5, 15, 77, 25, 35]
D[5, 15, 25, 77, 35]
Attempts:
2 left
💡 Hint
For even length arrays, middle index is length/2, which is 2 here.
🔧 Debug
advanced
2:00remaining
Identify 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));
ANullPointerException
BNo error, outputs [1, 2, 50, 3, 4, 5]
CCompilation error due to missing semicolon
DArrayIndexOutOfBoundsException
Attempts:
2 left
💡 Hint
Check how the original array is accessed inside the loop when i > middle.
❓ Predict Output
advanced
2:00remaining
What 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));
A[]
B[0]
C[null]
D[0, 0]
Attempts:
2 left
💡 Hint
An empty array has length 0, so middle index is 0.
🧠 Conceptual
expert
1:00remaining
How 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?
An + 1
Bn
Cn - 1
D2 * n
Attempts:
1 left
💡 Hint
Inserting means adding one new element, so the size changes accordingly.

Practice

(1/5)
1. What happens when you insert an element at the middle index of an array in Java?
easy
A. The array size automatically increases without shifting elements.
B. The new element replaces the element at the middle index without shifting.
C. Elements after the middle index shift right to make space for the new element.
D. The element is added at the end of the array regardless of the index.

Solution

  1. Step 1: Understand array insertion behavior

    In Java, arrays have fixed size, so to insert an element in the middle, elements from that index onward must shift right to make space.
  2. Step 2: Effect of insertion at middle index

    The element at the middle index and all elements after it move one position to the right, making room for the new element.
  3. Final Answer:

    Elements after the middle index shift right to make space for the new element. -> Option C
  4. Quick Check:

    Insertion shifts elements right [OK]
Hint: Insertion shifts elements right from middle index [OK]
Common Mistakes:
  • Thinking the new element replaces existing one without shifting
  • Assuming array size grows automatically
  • Believing insertion adds element at the end always
2. Which of the following Java code snippets correctly inserts an element `x` at the middle index of an integer array `arr` of size `n`?
easy
A. int mid = n / 2; for (int i = n - 1; i >= mid; i--) arr[i + 1] = arr[i]; arr[mid] = x;
B. int mid = n / 2; for (int i = 0; i < mid; i++) arr[i] = arr[i + 1]; arr[mid] = x;
C. int mid = n / 2; arr[mid] = x; for (int i = mid + 1; i < n; i++) arr[i] = arr[i];
D. int mid = n / 2; arr[mid + 1] = x; for (int i = mid; i < n; i++) arr[i] = arr[i + 1];

Solution

  1. Step 1: Identify correct shifting direction

    To insert at middle, elements from middle to end must shift right, so loop runs backward from end to middle.
  2. Step 2: Verify code correctness

    int mid = n / 2; for (int i = n - 1; i >= mid; i--) arr[i + 1] = arr[i]; arr[mid] = x; correctly shifts elements right starting from the last index to middle, then inserts x at mid.
  3. Final Answer:

    int mid = n / 2; for (int i = n - 1; i >= mid; i--) arr[i + 1] = arr[i]; arr[mid] = x; -> Option A
  4. Quick Check:

    Backward loop shifts right, then insert [OK]
Hint: Shift elements right backward from end to middle [OK]
Common Mistakes:
  • Shifting elements left instead of right
  • Looping forward causing overwrite
  • Assigning new element before shifting
3. What is the output of the following Java code?
int[] arr = {1, 2, 3, 4, 5, 0};
int n = 5;
int x = 99;
int mid = n / 2;
for (int i = n - 1; i >= mid; i--) {
    arr[i + 1] = arr[i];
}
arr[mid] = x;
for (int i = 0; i <= n; i++) {
    System.out.print(arr[i] + " ");
}
medium
A. 1 2 99 3 4 5
B. 1 2 3 99 4 5
C. 5 4 3 99 2 1
D. 1 2 99 3 4 5 0

Solution

  1. Step 1: Calculate middle index and shift elements

    n=5, mid=2. Loop shifts elements from index 4 to 2 right by one: arr[5]=arr[4]=5, arr[4]=arr[3]=4, arr[3]=arr[2]=3.
  2. Step 2: Insert x at middle and print array

    arr[2]=99. Final array: [1, 2, 99, 3, 4, 5]. Printing elements from 0 to 5 outputs: 1 2 99 3 4 5 0
  3. Final Answer:

    1 2 99 3 4 5 0 -> Option D
  4. Quick Check:

    Shift right + insert at mid = 1 2 99 3 4 5 0 [OK]
Hint: Shift right from end to mid, then insert new element [OK]
Common Mistakes:
  • Forgetting to shift elements before insertion
  • Printing extra zero at end
  • Incorrect middle index calculation
4. The following code attempts to insert element `x` at the middle index of array `arr` but produces incorrect output. What is the error?
int mid = arr.length / 2;
for (int i = mid; i < arr.length; i++) {
    arr[i + 1] = arr[i];
}
arr[mid] = x;
medium
A. Loop shifts elements left instead of right causing overwrite.
B. Loop runs forward causing overwriting of elements during shift.
C. Middle index calculation is incorrect for even length arrays.
D. New element is inserted before shifting elements.

Solution

  1. Step 1: Analyze loop direction and effect

    The loop runs forward from mid to end, assigning arr[i+1] = arr[i], which overwrites elements before they are copied.
  2. Step 2: Correct approach for shifting elements

    To avoid overwrite, elements must be shifted right starting from the end backward to mid.
  3. Final Answer:

    Loop runs forward causing overwriting of elements during shift. -> Option B
  4. Quick Check:

    Shift must go backward to avoid overwrite [OK]
Hint: Shift elements backward from end to mid to avoid overwrite [OK]
Common Mistakes:
  • Shifting elements forward causing data loss
  • Misunderstanding loop boundaries
  • Inserting element before shifting
5. You have an array `arr` of size `n` with an extra space at the end (size `n+1`). Write the steps to insert an element `x` exactly in the middle of the array, shifting elements correctly, and print the final array. Which of the following sequences of steps is correct?
hard
A. Calculate middle index; shift elements right from end to middle; insert x at middle; print array from 0 to n.
B. Calculate middle index; insert x at middle; shift elements right from middle to end; print array from 0 to n.
C. Calculate middle index; shift elements left from middle to end; insert x at middle; print array from 0 to n-1.
D. Calculate middle index; insert x at end; shift elements right from middle to end; print array from 0 to n.

Solution

  1. Step 1: Calculate middle index and prepare for insertion

    Find middle index as n/2. Since array has extra space, shifting elements right from end to middle creates space.
  2. Step 2: Shift elements right and insert new element

    Shift elements starting from last valid index backward to middle index to avoid overwriting. Insert x at middle index.
  3. Step 3: Print the updated array

    Print elements from index 0 to n (inclusive) to show the array with inserted element.
  4. Final Answer:

    Calculate middle index; shift elements right from end to middle; insert x at middle; print array from 0 to n. -> Option A
  5. Quick Check:

    Shift right backward, insert at mid, print full array [OK]
Hint: Shift right backward, insert at mid, print full array [OK]
Common Mistakes:
  • Inserting before shifting elements
  • Shifting elements forward causing overwrite
  • Printing wrong array range
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard