Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting elements at the end of an array
What is the output of the following C++ code that inserts elements at the end of an array?
DSA C++
#include <iostream> using namespace std; int main() { int arr[5] = {1, 2, 3}; int size = 3; int capacity = 5; if (size < capacity) { arr[size] = 4; size++; } if (size < capacity) { arr[size] = 5; size++; } for (int i = 0; i < size; i++) { cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check how many times the insertion condition is true and how size changes.
✗ Incorrect
The code inserts 4 and then 5 at the end of the array because size is less than capacity twice. The final array printed is 1 2 3 4 5.
❓ Predict Output
intermediateResulting array after conditional insertion
What will be the output of this code snippet that tries to insert an element at the end only if there is space?
DSA C++
#include <iostream> using namespace std; int main() { int arr[4] = {10, 20, 30, 40}; int size = 4; int capacity = 4; if (size < capacity) { arr[size] = 50; size++; } for (int i = 0; i < size; i++) { cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check if the insertion condition is true or false.
✗ Incorrect
Since size equals capacity, the insertion condition fails and no new element is added. The array remains unchanged.
🔧 Debug
advancedIdentify the error in array insertion code
What error will this code produce when trying to insert an element at the end of the array?
DSA C++
#include <iostream> using namespace std; int main() { int arr[3] = {5, 10, 15}; int size = 3; int capacity = 3; arr[size] = 20; size++; for (int i = 0; i < size; i++) { cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check if the code respects the array capacity before insertion.
✗ Incorrect
The code writes to arr[3] which is outside the declared array size of 3, causing undefined behavior or runtime error.
🧠 Conceptual
advancedUnderstanding array insertion at end with fixed size
Why is it important to check the array capacity before inserting an element at the end?
Attempts:
2 left
💡 Hint
Think about what happens if you write beyond the array size in memory.
✗ Incorrect
Accessing or writing outside the array bounds can cause crashes, unpredictable behavior, or security issues.
🚀 Application
expertPredict the final array after multiple insertions with capacity check
Given the following code, what is the final printed array?
DSA C++
#include <iostream> using namespace std; int main() { int arr[6] = {2, 4, 6}; int size = 3; int capacity = 6; int to_insert[] = {8, 10, 12, 14}; int n = 4; for (int i = 0; i < n; i++) { if (size < capacity) { arr[size] = to_insert[i]; size++; } } for (int i = 0; i < size; i++) { cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check how many elements can fit given the capacity and initial size.
✗ Incorrect
The array capacity is 6, initial size 3, so only 3 elements from to_insert can be added. The last element 14 is not inserted.
