Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after inserting element in the middle of an array
What is the output of the following C code after inserting the value 99 at the middle index of the array?
DSA C
int main() { int arr[6] = {10, 20, 30, 40, 50}; int n = 5; int val = 99; int mid = n / 2; for (int i = n; i > mid; i--) { arr[i] = arr[i - 1]; } arr[mid] = val; n++; for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Remember that integer division truncates, so middle index is n/2 = 2 for n=5.
✗ Incorrect
The middle index for an array of size 5 is 2 (0-based). The code shifts elements from the end to the middle index to the right by one position, then inserts 99 at index 2. The resulting array is: 10 20 99 30 40 50.
🧠 Conceptual
intermediate1:30remaining
Understanding middle index calculation for array insertion
Given an array of length n, which formula correctly calculates the middle index to insert a new element so that the array remains balanced?
Attempts:
2 left
💡 Hint
Consider zero-based indexing and integer division truncation.
✗ Incorrect
For zero-based arrays, the middle index is calculated as n / 2 using integer division. This places the new element roughly in the center, balancing elements on both sides.
🔧 Debug
advanced2:00remaining
Identify the error in array insertion code
What error will occur when running this code snippet that inserts an element at the middle of an array of size 5?
int arr[5] = {1, 2, 3, 4, 5};
int n = 5;
int val = 10;
int mid = n / 2;
for (int i = n; i > mid; i--) {
arr[i] = arr[i - 1];
}
arr[mid] = val;
n++;
Attempts:
2 left
💡 Hint
Check the array size and the index used in the loop.
✗ Incorrect
The array size is 5, but the loop writes to arr[5], which is outside the allocated memory, causing buffer overflow.
❓ Predict Output
advanced2:30remaining
Output after multiple insertions at middle index
What is the output after inserting values 99 and then 77 at the middle index of the array?
DSA C
int main() { int arr[8] = {1, 2, 3, 4, 5, 6}; int n = 6; int val1 = 99; int val2 = 77; int mid = n / 2; // Insert 99 for (int i = n; i > mid; i--) { arr[i] = arr[i - 1]; } arr[mid] = val1; n++; // Insert 77 mid = n / 2; for (int i = n; i > mid; i--) { arr[i] = arr[i - 1]; } arr[mid] = val2; n++; for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Remember to recalculate middle index after first insertion.
✗ Incorrect
After first insertion, array is: 1 2 3 99 4 5 6
New size n=7, middle index = 7/2=3
Second insertion shifts elements from index 3, inserts 77 at index 3.
Final array: 1 2 3 77 99 4 5 6
🧠 Conceptual
expert1:30remaining
Time complexity of inserting an element at the middle of an array
What is the time complexity of inserting an element at the middle index of an array of size n, assuming shifting elements is required?
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved to make space.
✗ Incorrect
Inserting at the middle requires shifting about half the elements, which is proportional to n, so time complexity is O(n).
