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 array
What is the output of the following C# code after deleting the middle element from the array?
DSA C#
int[] arr = {10, 20, 30, 40, 50}; int mid = arr.Length / 2; int[] result = new int[arr.Length - 1]; for (int i = 0, j = 0; i < arr.Length; i++) { if (i != mid) { result[j++] = arr[i]; } } foreach (int val in result) { Console.Write(val + " "); }
Attempts:
2 left
💡 Hint
Remember that the middle index for an odd-length array is length divided by 2.
✗ Incorrect
The middle index is 2 (0-based), so element 30 is removed. The resulting array is [10, 20, 40, 50].
❓ Predict Output
intermediateResulting array length after middle deletion
Given an array of length 6, what is the length of the array after deleting the middle element using the standard middle index calculation?
DSA C#
int[] arr = {5, 10, 15, 20, 25, 30}; int mid = arr.Length / 2; int[] result = new int[arr.Length - 1]; // deletion logic omitted for brevity Console.WriteLine(result.Length);
Attempts:
2 left
💡 Hint
Deleting one element reduces the length by one.
✗ Incorrect
Original length is 6, after deleting one element length becomes 5.
🔧 Debug
advancedIdentify the error in middle deletion code
What error will this C# code produce when trying to delete the middle element from an array?
DSA C#
int[] arr = {1, 2, 3, 4, 5}; int mid = arr.Length / 2; int[] result = new int[arr.Length - 1]; for (int i = 0; i < arr.Length; i++) { if (i != mid) { result[i] = arr[i]; } } foreach (int val in result) { Console.Write(val + " "); }
Attempts:
2 left
💡 Hint
Check how the index for the result array is used inside the loop.
✗ Incorrect
The code uses 'i' as index for 'result' array, but 'result' is smaller. When i reaches the last index, it causes IndexOutOfRangeException.
🚀 Application
advancedDeleting middle element from even-length array
If you have an even-length array {2, 4, 6, 8}, which element is removed when deleting the middle element using mid = length / 2?
Attempts:
2 left
💡 Hint
Integer division truncates the decimal part.
✗ Incorrect
Length is 4, mid = 4 / 2 = 2, so element at index 2 (which is 6) is removed.
🧠 Conceptual
expertTime complexity of deleting middle element in array
What is the time complexity of deleting the middle element from an array of size n by shifting elements?
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved after deletion.
✗ Incorrect
Deleting the middle element requires shifting all elements after it one position left, which takes linear time O(n).
