Bird
0
0
DSA Cprogramming~20 mins

Array Insertion at Middle Index in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A10 20 30 40 99 50
B10 20 30 99 40 50
C10 20 99 30 40 50
D10 20 30 40 50 99
Attempts:
2 left
💡 Hint
Remember that integer division truncates, so middle index is n/2 = 2 for n=5.
🧠 Conceptual
intermediate
1: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?
Amiddle = (n + 1) / 2
Bmiddle = (n - 1) / 2
Cmiddle = n - 1
Dmiddle = n / 2
Attempts:
2 left
💡 Hint
Consider zero-based indexing and integer division truncation.
🔧 Debug
advanced
2: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++;
ABuffer overflow (writing outside array bounds)
BNo error, runs correctly
CSegmentation fault due to null pointer
DCompilation error due to syntax
Attempts:
2 left
💡 Hint
Check the array size and the index used in the loop.
Predict Output
advanced
2: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;
}
A1 2 3 77 99 4 5 6
B1 2 3 99 77 4 5 6
C1 2 3 99 4 77 5 6
D1 2 3 4 77 99 5 6
Attempts:
2 left
💡 Hint
Remember to recalculate middle index after first insertion.
🧠 Conceptual
expert
1: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?
AO(1)
BO(n)
CO(log n)
DO(n^2)
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved to make space.