Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting elements at the end of the array?
Consider the following Java code that inserts elements at the end of an array. What will be printed?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = new int[5]; int size = 0; // Insert 10 arr[size++] = 10; // Insert 20 arr[size++] = 20; // Insert 30 arr[size++] = 30; // Print array elements up to size for (int i = 0; i < size; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
Focus on how many elements are printed and what the size variable controls.
✗ Incorrect
The code inserts three elements at the end of the array and prints only those inserted elements using the size variable as the limit.
❓ Predict Output
intermediateWhat happens if you insert beyond the array size?
What error or output will this Java code produce when inserting beyond the array's capacity?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = new int[3]; int size = 0; arr[size++] = 1; arr[size++] = 2; arr[size++] = 3; arr[size++] = 4; // Inserting beyond capacity for (int i = 0; i < size; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
Think about what happens when you try to access an index outside the array length.
✗ Incorrect
The array has size 3, so trying to insert at index 3 causes ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedWhy is tracking 'size' important when inserting at the end of an array?
In array insertion at the end, why do we keep a separate 'size' variable instead of relying on array.length?
Attempts:
2 left
💡 Hint
Think about the difference between capacity and actual data stored.
✗ Incorrect
The array length is fixed and shows total capacity; size tracks how many elements are actually inserted.
❓ Predict Output
advancedWhat is the output after multiple insertions and printing the array?
What will be printed after running this Java code that inserts elements at the end and prints the array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = new int[4]; int size = 0; arr[size++] = 5; arr[size++] = 10; arr[size++] = 15; arr[size++] = 20; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
The array is fully filled; printing all elements shows all inserted values.
✗ Incorrect
All four elements are inserted and printed because size equals array length.
🔧 Debug
expertIdentify the bug in this array insertion code
What is the bug in this Java code that inserts elements at the end of an array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = new int[3]; int size = 0; insert(arr, size, 1); insert(arr, size, 2); insert(arr, size, 3); for (int i = 0; i < size; i++) { System.out.print(arr[i] + " "); } } public static void insert(int[] arr, int size, int value) { arr[size] = value; size++; } }
Attempts:
2 left
💡 Hint
Consider how Java passes primitive variables to methods.
✗ Incorrect
The size variable is passed by value, so incrementing it inside insert does not affect the original size in main.
