Challenge - 5 Problems
Array Access and Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Array Update and Access
What is the output of the following Java code snippet?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {5, 10, 15, 20}; arr[2] = arr[1] + arr[3]; System.out.print(arr[2] + ", " + arr[0] + ", " + arr[3]); } }
Attempts:
2 left
💡 Hint
Remember that arr[2] is updated by adding arr[1] and arr[3].
✗ Incorrect
arr[2] = arr[1] + arr[3] = 10 + 20 = 30. The output prints arr[2], arr[0], arr[3] which are 30, 5, 20 respectively.
❓ Predict Output
intermediateResulting Array After Multiple Updates
What is the state of the array after executing this Java code?
DSA Java
public class Main { public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5}; nums[0] = nums[4]; nums[4] = nums[2]; nums[2] = nums[1]; for (int num : nums) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Track each assignment step carefully in order.
✗ Incorrect
Step 1: nums[0] = nums[4] → nums[0] = 5
Step 2: nums[4] = nums[2] → nums[4] = 3
Step 3: nums[2] = nums[1] → nums[2] = 2
Final array: {5, 2, 2, 4, 3}
🔧 Debug
advancedIdentify the Error in Array Update
What error will this Java code produce when run?
DSA Java
public class Main { public static void main(String[] args) { int[] data = new int[3]; data[0] = 10; data[1] = 20; data[3] = 30; System.out.println(data[2]); } }
Attempts:
2 left
💡 Hint
Check the array size and the index used for assignment.
✗ Incorrect
The array has size 3 with valid indices 0,1,2. Accessing data[3] causes ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedOutput After Conditional Array Update
What is the output of this Java program?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {2, 4, 6, 8}; for (int i = 0; i < arr.length; i++) { if (i % 2 == 0) { arr[i] = arr[i] * 2; } else { arr[i] = arr[i] + 1; } } for (int val : arr) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
Multiply elements at even indices by 2, add 1 to odd indices.
✗ Incorrect
Index 0 (even): 2*2=4
Index 1 (odd): 4+1=5
Index 2 (even): 6*2=12
Index 3 (odd): 8+1=9
Output: 4 5 12 9
🧠 Conceptual
expertArray Update and Access Complexity
Consider an array of size n. What is the time complexity of updating an element at a specific index and then accessing it?
Attempts:
2 left
💡 Hint
Think about how arrays store elements and how indexing works.
✗ Incorrect
Arrays allow direct access to any index in constant time, so both update and access are O(1).
