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 first element from array
What is the output of the following Java code that deletes the first element from an array and shifts the remaining elements left?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50}; int n = arr.length; for (int i = 1; i < n; i++) { arr[i - 1] = arr[i]; } n = n - 1; for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
Focus on how elements are shifted and the new length after deletion.
✗ Incorrect
The code shifts all elements one position to the left, overwriting the first element. The length is reduced by one, so the last element is not printed. The output is the array without the first element: 20 30 40 50.
❓ Predict Output
intermediateResulting array after deleting first element and printing full length
What will be printed by this Java code that deletes the first element but prints the entire array length?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {5, 15, 25, 35}; int n = arr.length; for (int i = 1; i < n; i++) { arr[i - 1] = arr[i]; } n = n - 1; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
Notice the loop prints the original array length, not the reduced length.
✗ Incorrect
The elements are shifted left, so arr becomes [15, 25, 35, 35]. The last element is not cleared, so it remains 35. Printing full length shows 15 25 35 35.
🧠 Conceptual
advancedTime complexity of deleting first element in array
What is the time complexity of deleting the first element from an array by shifting all elements one position to the left?
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved when deleting the first element.
✗ Incorrect
Deleting the first element requires shifting all remaining elements one position left, which takes time proportional to the number of elements, so O(n).
🔧 Debug
advancedIdentify the error in array deletion code
What error will this Java code produce when deleting the first element from an array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3}; int n = arr.length; for (int i = 0; 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
Check the loop boundary when accessing arr[i + 1].
✗ Incorrect
The loop runs from i=0 to i=2 (n=3), so at i=2 it tries to access arr[3], which is out of bounds, causing ArrayIndexOutOfBoundsException.
🚀 Application
expertEfficient data structure for frequent deletions at beginning
If you need to delete the first element frequently from a collection, which data structure is best to use for efficient deletions?
Attempts:
2 left
💡 Hint
Consider which structure allows O(1) deletion at the front without shifting elements.
✗ Incorrect
Linked lists allow deletion of the first element in O(1) time by adjusting the head pointer, unlike arrays which require shifting elements.
