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 99 at the middle index?
Given the array and insertion code below, what is the printed array after inserting 99 at the middle index?
DSA C++
int arr[6] = {10, 20, 30, 40, 50}; int n = 5; int x = 99; int mid = n / 2; for (int i = n; i > mid; i--) { arr[i] = arr[i - 1]; } arr[mid] = x; n++; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Remember that the middle index is calculated as n/2 and elements after it shift right.
✗ Incorrect
The middle index is 2 (5/2). Elements from index 2 to 4 shift right by one. Then 99 is placed at index 2.
❓ Predict Output
intermediateWhat is the output after inserting 7 at the middle index?
Consider the array and insertion code below. What is the printed array after inserting 7 at the middle index?
DSA C++
int arr[7] = {1, 2, 3, 4, 5, 6}; int n = 6; int x = 7; int mid = n / 2; for (int i = n; i > mid; i--) { arr[i] = arr[i - 1]; } arr[mid] = x; n++; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Calculate middle index as n/2 and shift elements after it right.
✗ Incorrect
Middle index is 3 (6/2). Elements from index 3 to 5 shift right. Insert 7 at index 3.
🔧 Debug
advancedWhat error occurs when inserting at middle index with insufficient array size?
Given the code below, what error will occur when trying to insert 100 at the middle index?
DSA C++
int arr[5] = {1, 2, 3, 4, 5}; int n = 5; int x = 100; int mid = n / 2; for (int i = n; i > mid; i--) { arr[i] = arr[i - 1]; } arr[mid] = x; n++; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Check array size and index used in the loop.
✗ Incorrect
The array has size 5, but the loop writes to arr[5], which is out of bounds causing runtime error.
❓ Predict Output
advancedWhat is the output after inserting 0 at the middle index in an even-sized array?
Given the array and insertion code below, what is the printed array after inserting 0 at the middle index?
DSA C++
int arr[8] = {5, 10, 15, 20, 25, 30, 35}; int n = 7; int x = 0; int mid = n / 2; for (int i = n; i > mid; i--) { arr[i] = arr[i - 1]; } arr[mid] = x; n++; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Middle index is n/2 = 3, shift elements from index 3 right.
✗ Incorrect
Middle index is 3. Elements from index 3 to 6 shift right. Insert 0 at index 3.
🧠 Conceptual
expertHow many elements are in the array after inserting at the middle index?
If an array initially has n elements and you insert one element at the middle index, how many elements does the array have afterward?
Attempts:
1 left
💡 Hint
Inserting adds one new element to the array.
✗ Incorrect
Inserting one element increases the total count by one, so the array has n + 1 elements.
