Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after inserting an element at the end?
Consider the following C code that inserts an element at the end of an array. What will be printed after insertion?
DSA C
int main() { int arr[5] = {1, 2, 3, 4}; int size = 4; int capacity = 5; int element = 10; if (size < capacity) { arr[size] = element; size++; } for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Think about how the element is added and how the size changes.
✗ Incorrect
The element 10 is added at index 4 (the end), and size is incremented to 5. The loop prints all 5 elements.
❓ Predict Output
intermediate2:00remaining
What happens if we insert beyond array capacity?
What will be the output of this C code snippet?
DSA C
int main() { int arr[3] = {5, 6, 7}; int size = 3; int capacity = 3; int element = 9; if (size < capacity) { arr[size] = element; size++; } for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Check the condition before insertion carefully.
✗ Incorrect
Since size equals capacity, the insertion does not happen. The array remains unchanged with 3 elements.
🔧 Debug
advanced2:00remaining
Why does this insertion code cause unexpected output?
Identify the problem in this code that inserts an element at the end of an array and prints it.
DSA C
int main() { int arr[4] = {1, 2, 3}; int size = 3; int capacity = 4; int element = 8; arr[size] = element; // Missing size increment here for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Check if the size variable is updated after insertion.
✗ Incorrect
The element is assigned at arr[3], but size is not incremented, so the loop prints only first 3 elements.
❓ Predict Output
advanced2:00remaining
What is the output after multiple insertions at the end?
Given the following code, what will be printed?
DSA C
int main() { int arr[6] = {2, 4, 6}; int size = 3; int capacity = 6; int elements_to_add[3] = {8, 10, 12}; for (int i = 0; i < 3; i++) { if (size < capacity) { arr[size] = elements_to_add[i]; size++; } } for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Look at how the loop adds elements and updates size.
✗ Incorrect
All three elements 8, 10, 12 are added at the end, size becomes 6, and all elements print.
🧠 Conceptual
expert2:00remaining
What is the main limitation of inserting at the end of a static array?
Why can inserting an element at the end of a static array fail or be limited?
Attempts:
2 left
💡 Hint
Think about the difference between static and dynamic arrays.
✗ Incorrect
Static arrays have fixed size; once full, no more elements can be added without creating a new array.
