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 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]); }
Attempts:
2 left
💡 Hint
Think about shifting elements from the end towards the beginning before inserting.
✗ Incorrect
To insert at the beginning, all elements must move one position right. Then the new element is placed at index 0.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about what happens if you put a new element at index 0 without moving others.
✗ Incorrect
Shifting elements right creates an empty slot at index 0 so the new element can be inserted without overwriting existing data.
🔧 Debug
advanced2: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]); }
Attempts:
2 left
💡 Hint
Consider what happens when you move elements forward starting from the beginning.
✗ Incorrect
Shifting elements forward from the start overwrites data before it is copied. Looping backwards preserves data during shifting.
❓ Predict Output
advanced2: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]); }
Attempts:
2 left
💡 Hint
Each new element goes to index 0, pushing others right.
✗ Incorrect
Insert 3: [3]
Insert 7: [7, 3]
Insert 1: [1, 7, 3]
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how many elements must be moved when inserting at the start.
✗ Incorrect
Each insertion at the beginning requires moving all existing elements one position right, which takes time proportional to the number of elements.
