Bird
0
0
DSA Cprogramming~20 mins

Array Insertion at Beginning 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
What is the output after inserting at the beginning?
Consider an array of integers initially containing [10, 20, 30, 40]. We insert the value 5 at the beginning by shifting elements to the right. What is the final array?
DSA C
int arr[5] = {10, 20, 30, 40};
int n = 4;
int x = 5;
for (int i = n; i > 0; i--) {
    arr[i] = arr[i-1];
}
arr[0] = x;
n++;
for (int i = 0; i < n; i++) {
    printf("%d ", arr[i]);
}
A[5, 20, 30, 40, 10]
B[10, 5, 20, 30, 40]
C[5, 10, 20, 30, 40]
D[10, 20, 30, 40, 5]
Attempts:
2 left
💡 Hint
Think about shifting elements from the end towards the beginning before inserting.
🧠 Conceptual
intermediate
1:30remaining
Why do we shift elements when inserting at the beginning?
When inserting a new element at the start of a fixed-size array, why is it necessary to shift existing elements to the right?
ATo sort the array automatically after insertion.
BTo make space at the beginning without losing existing elements.
CTo remove duplicates from the array.
DTo reverse the array order.
Attempts:
2 left
💡 Hint
Think about what happens if you put a new element at index 0 without moving others.
🔧 Debug
advanced
2:30remaining
Find the error in this insertion code
This code attempts to insert 99 at the beginning of an array but produces wrong output. What is the error?
DSA C
int arr[5] = {1, 2, 3, 4};
int n = 4;
int x = 99;
for (int i = 0; i < n; i++) {
    arr[i+1] = arr[i];
}
arr[0] = x;
n++;
for (int i = 0; i < n; i++) {
    printf("%d ", arr[i]);
}
AThe loop should run backwards from n to 0 to avoid overwriting elements.
BThe array size is too small to hold the new element.
CThe value x should be assigned after the loop ends.
DThe loop condition should be i <= n instead of i < n.
Attempts:
2 left
💡 Hint
Consider what happens when you move elements forward starting from the beginning.
Predict Output
advanced
2:30remaining
Output after multiple insertions at beginning
Starting with an empty array of size 5, insert 3, then 7, then 1 at the beginning in that order. What is the final array?
DSA C
int arr[5];
int n = 0;
int values[] = {3, 7, 1};
for (int k = 0; k < 3; k++) {
    for (int i = n; i > 0; i--) {
        arr[i] = arr[i-1];
    }
    arr[0] = values[k];
    n++;
}
for (int i = 0; i < n; i++) {
    printf("%d ", arr[i]);
}
A[1, 7, 3]
B[3, 7, 1]
C[7, 3, 1]
D[1, 3, 7]
Attempts:
2 left
💡 Hint
Each new element goes to index 0, pushing others right.
🧠 Conceptual
expert
2:00remaining
Why is inserting at the beginning of an array costly?
Inserting an element at the beginning of a large fixed-size array requires shifting all elements. Why is this operation considered costly in terms of performance?
ABecause inserting at the beginning causes the array to resize automatically.
BBecause arrays cannot store elements at the beginning.
CBecause the first element is always locked and cannot be changed.
DBecause shifting elements requires moving many items, which takes time proportional to the array size.
Attempts:
2 left
💡 Hint
Think about how many elements must be moved when inserting at the start.