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 at the beginning?
Consider the following C# code that inserts a value at the beginning of an array by shifting elements. What will be printed?
DSA C#
int[] arr = {2, 3, 4, 5}; int newValue = 1; int n = arr.Length; int[] newArr = new int[n + 1]; for (int i = 0; i < n; i++) { newArr[i + 1] = arr[i]; } newArr[0] = newValue; for (int i = 0; i < newArr.Length; i++) { Console.Write(newArr[i] + " "); }
Attempts:
2 left
💡 Hint
Think about how elements are shifted to the right to make space at the start.
✗ Incorrect
The code creates a new array with one extra space. It shifts all original elements one position right and inserts the new value at index 0. So the output is the new value followed by the original elements.
❓ Predict Output
intermediateWhat error occurs when inserting without resizing?
What happens if you try to insert an element at the beginning of a fixed-size array without creating a new array or resizing it?
DSA C#
int[] arr = {2, 3, 4, 5}; int newValue = 1; for (int i = arr.Length - 1; i > 0; i--) { arr[i] = arr[i - 1]; } arr[0] = newValue; for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + " "); }
Attempts:
2 left
💡 Hint
Arrays in C# have fixed size. Shifting elements overwrites the last element.
✗ Incorrect
The code shifts elements right inside the same array, overwriting the last element (5). So the last element is lost and the array becomes {1, 2, 3, 4}. No error occurs because the array size is fixed.
🔧 Debug
advancedWhy does this insertion code cause an error?
This code tries to insert a value at the beginning of an array but causes a runtime error. What is the cause?
DSA C#
int[] arr = {2, 3, 4, 5}; int newValue = 1; for (int i = 0; i < arr.Length; i++) { arr[i + 1] = arr[i]; } arr[0] = newValue;
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs from 0 to arr.Length exclusive, but arr[i + 1] accesses beyond the array size when i = arr.Length - 1, causing IndexOutOfRangeException.
🧠 Conceptual
advancedWhat is the time complexity of inserting at the beginning of an array?
If you insert an element at the beginning of an array by shifting all elements one position to the right, what is the time complexity in terms of n (number of elements)?
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved for each insertion.
✗ Incorrect
Each insertion at the beginning requires shifting all existing elements one position to the right, which takes time proportional to n.
🚀 Application
expertHow to efficiently insert multiple elements at the beginning?
You need to insert multiple elements at the beginning of a large array repeatedly. Which data structure or approach is best to optimize insertion performance?
Attempts:
2 left
💡 Hint
Think about which data structure allows fast insertion at the start without shifting.
✗ Incorrect
Linked lists allow insertion at the beginning in constant time without shifting elements, unlike arrays which require shifting.
