Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the first element from the array?
Consider the following C# code that deletes the first element from an array by shifting elements left. What will be printed?
DSA C#
int[] arr = {10, 20, 30, 40, 50}; int n = arr.Length; for (int i = 1; i < n; i++) { arr[i - 1] = arr[i]; } n--; for (int i = 0; i < n; i++) { Console.Write(arr[i] + " "); }
Attempts:
2 left
💡 Hint
Think about how elements are shifted and the new length after deletion.
✗ Incorrect
The first element (10) is removed by shifting all elements left. The new array length is one less, so elements 20, 30, 40, 50 remain and are printed.
❓ Predict Output
intermediateWhat happens if we delete the first element twice in a row?
Given the array and deletion code below, what will be the output after deleting the first element two times?
DSA C#
int[] arr = {5, 15, 25, 35, 45}; int n = arr.Length; for (int d = 0; d < 2; d++) { for (int i = 1; i < n; i++) { arr[i - 1] = arr[i]; } n--; } for (int i = 0; i < n; i++) { Console.Write(arr[i] + " "); }
Attempts:
2 left
💡 Hint
Each deletion shifts elements left and reduces length by one.
✗ Incorrect
First deletion removes 5, array becomes 15 25 35 45. Second deletion removes 15, array becomes 25 35 45.
🔧 Debug
advancedWhy does this code cause an error when deleting the first element?
Examine the code below. It attempts to delete the first element by shifting elements left. What error will it cause and why?
DSA C#
int[] arr = {1, 2, 3}; int n = arr.Length; for (int i = 0; i < n; i++) { arr[i] = arr[i + 1]; } n--; for (int i = 0; i < n; i++) { Console.Write(arr[i] + " "); }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs from i=0 to i=n-1. At i=n-1, arr[i + 1] is arr[n], which is out of array bounds causing IndexOutOfRangeException.
🧠 Conceptual
advancedWhat is the time complexity of deleting the first element from an array by shifting?
If you delete the first element of an array by shifting all other elements one position to the left, what is the time complexity in terms of n (array size)?
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved.
✗ Incorrect
All elements except the first must be shifted left by one, so the operation takes time proportional to n.
🚀 Application
expertAfter deleting the first element from an array, what is the state of the array?
Given the array arr = {7, 14, 21, 28} and code that deletes the first element by shifting left and reducing length by one, what is the printed output?
DSA C#
int[] arr = {7, 14, 21, 28}; int n = arr.Length; for (int i = 1; i < n; i++) { arr[i - 1] = arr[i]; } n--; for (int i = 0; i < n; i++) { Console.Write(arr[i] + " -> "); } Console.Write("null");
Attempts:
2 left
💡 Hint
Focus on how elements shift and the new length.
✗ Incorrect
The first element 7 is removed by shifting left. The new length is 3, so elements 14, 21, 28 remain and are printed with arrows.
