Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting middle element from an odd-length array
What is the output of the following Java code after deleting the middle element from the array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50}; int n = arr.length; int mid = n / 2; for (int i = mid; i < n - 1; i++) { arr[i] = arr[i + 1]; } n = n - 1; for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
Think about how elements after the middle index shift left by one position.
✗ Incorrect
The middle element 30 at index 2 is removed. Elements after index 2 shift left, so 40 and 50 move to indices 2 and 3. The new array length is 4, so output is 10 20 40 50.
❓ Predict Output
intermediateOutput after deleting middle element from an even-length array
What is the output of this Java code after deleting the middle element from an even-length array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {5, 15, 25, 35, 45, 55}; int n = arr.length; int mid = n / 2; for (int i = mid; i < n - 1; i++) { arr[i] = arr[i + 1]; } n = n - 1; for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
For even length, middle index is n/2, which element is removed?
✗ Incorrect
Middle index is 3 (6/2). Element 35 at index 3 is removed. Elements after index 3 shift left, so 45 and 55 move left. New length is 5.
🔧 Debug
advancedIdentify the error in this array deletion code
What error will this Java code produce when trying to delete the middle element from the array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4}; int n = arr.length; int mid = (n - 1) / 2; for (int i = mid; i <= n; i++) { arr[i] = arr[i + 1]; } n = n - 1; for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs with i <= n and accesses arr[i + 1], which goes beyond array bounds causing ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedEffect of deleting middle element on array size and indexing
After deleting the middle element from an array of length n, which statement is true?
Attempts:
2 left
💡 Hint
Think about how arrays store elements continuously.
✗ Incorrect
Deleting an element reduces size by one and shifts subsequent elements left to fill the gap.
❓ Predict Output
expertOutput after multiple deletions of middle element
What is the output of this Java code after deleting the middle element twice in a row?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {2, 4, 6, 8, 10, 12, 14}; int n = arr.length; for (int count = 0; count < 2; count++) { int mid = n / 2; for (int i = mid; i < n - 1; i++) { arr[i] = arr[i + 1]; } n = n - 1; } for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
Delete middle element once, then again on the new array.
✗ Incorrect
First deletion removes element at index 3 (8). Array becomes {2,4,6,10,12,14}. Second deletion removes element at index 3 (10). Elements after shift left: 12 and 14 to indices 3 and 4. Final array {2,4,6,12,14}.
