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 last element from the array?
Consider the following Java code that deletes the last element from an array by reducing its size logically. What will be printed?
DSA Java
int[] arr = {10, 20, 30, 40, 50}; int size = arr.length; size--; for (int i = 0; i < size; i++) { System.out.print(arr[i] + " "); }
Attempts:
2 left
💡 Hint
Think about how reducing the size variable affects the loop.
✗ Incorrect
The size variable is reduced by one, so the loop prints all elements except the last one (50).
❓ Predict Output
intermediateWhat happens if we try to access the deleted element after reducing size?
Given the code below, what will be the output or error?
DSA Java
int[] arr = {5, 15, 25, 35}; int size = arr.length; size--; System.out.println(arr[size]);
Attempts:
2 left
💡 Hint
Remember size was decremented before accessing arr[size].
✗ Incorrect
After decrement, size is 3, so arr[3] is accessed which is 35.
🔧 Debug
advancedWhy does this code print the last element even after deletion?
Analyze the code below. It tries to delete the last element by reducing size, but the output still includes the last element. Why?
DSA Java
int[] arr = {1, 2, 3, 4, 5}; int size = arr.length; size = size - 1; for (int i = 0; i <= size; i++) { System.out.print(arr[i] + " "); }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop runs from i=0 to i=size inclusive, so it prints elements at indices 0 to size, including the last element.
🧠 Conceptual
advancedWhat is the main limitation of deleting the last element in a fixed-size array?
Why can't we physically remove the last element from a fixed-size array in Java?
Attempts:
2 left
💡 Hint
Think about how arrays are stored in memory.
✗ Incorrect
Arrays in Java have a fixed length set at creation time; you cannot change their size dynamically.
🚀 Application
expertHow to implement logical deletion of last element in an array?
You want to delete the last element from an array logically without creating a new array. Which approach is correct?
Attempts:
2 left
💡 Hint
Logical deletion means ignoring the element without changing the array size.
✗ Incorrect
By keeping a size variable and decrementing it, you can ignore the last element without reallocating the array.
