Challenge - 5 Problems
Array Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after updating an array element
What is the output of the following C++ code snippet?
DSA C++
int arr[5] = {10, 20, 30, 40, 50}; arr[2] = 100; for(int i = 0; i < 5; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0.
✗ Incorrect
The element at index 2 (third element) is updated from 30 to 100. The rest remain unchanged.
❓ Predict Output
intermediateValue of array element after multiple updates
What is the value of arr[1] after executing this code?
DSA C++
int arr[3] = {5, 10, 15}; arr[1] = arr[1] + 20; arr[1] = arr[1] - 5;
Attempts:
2 left
💡 Hint
Add and subtract carefully step by step.
✗ Incorrect
Initially arr[1] is 10. After adding 20, it becomes 30. Then subtracting 5 results in 25.
🔧 Debug
advancedIdentify the error in array update
What error will this code produce when compiled or run?
DSA C++
int arr[4] = {1, 2, 3, 4}; arr[4] = 10; std::cout << arr[4];
Attempts:
2 left
💡 Hint
Array indices start at 0 and go up to size-1.
✗ Incorrect
Index 4 is out of bounds for an array of size 4 (valid indices 0 to 3). Accessing arr[4] causes undefined behavior, often a runtime error.
❓ Predict Output
advancedFinal array state after multiple updates
What is the printed output after running this code?
DSA C++
int arr[6] = {1, 2, 3, 4, 5, 6}; for(int i = 0; i < 6; i++) { if(i % 2 == 0) { arr[i] = arr[i] * 2; } else { arr[i] = arr[i] + 3; } } for(int i = 0; i < 6; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Check even and odd indices separately.
✗ Incorrect
Even indices (0,2,4) are doubled: 1*2=2, 3*2=6, 5*2=10. Odd indices (1,3,5) add 3: 2+3=5, 4+3=7, 6+3=9.
🧠 Conceptual
expertEffect of pointer arithmetic on array update
Given the code below, what is the output after execution?
DSA C++
int arr[3] = {10, 20, 30}; int *p = arr; *(p + 1) = 50; std::cout << arr[0] << " " << arr[1] << " " << arr[2] << " ";
Attempts:
2 left
💡 Hint
Pointer arithmetic moves the pointer by element size.
✗ Incorrect
Pointer p points to arr[0]. *(p + 1) accesses arr[1], which is updated to 50. Others remain unchanged.
