Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting element in middle of array
What is the output of the following C# code that inserts an element in the middle of an array?
DSA C#
int[] arr = {1, 2, 4, 5}; int insertVal = 3; int mid = arr.Length / 2; int[] newArr = new int[arr.Length + 1]; for (int i = 0; i < mid; i++) { newArr[i] = arr[i]; } newArr[mid] = insertVal; for (int i = mid; i < arr.Length; i++) { newArr[i + 1] = arr[i]; } foreach (int val in newArr) { Console.Write(val + " "); }
Attempts:
2 left
💡 Hint
Think about how the elements after the middle index are shifted to the right.
✗ Incorrect
The code inserts 3 at the middle index (2) and shifts elements 4 and 5 to the right, resulting in 1 2 3 4 5.
❓ Predict Output
intermediateResulting array after middle insertion with even length
Given the code below, what will be printed after inserting 99 in the middle of the array?
DSA C#
int[] arr = {10, 20, 30, 40, 50, 60}; int insertVal = 99; int mid = arr.Length / 2; int[] newArr = new int[arr.Length + 1]; for (int i = 0; i < mid; i++) { newArr[i] = arr[i]; } newArr[mid] = insertVal; for (int i = mid; i < arr.Length; i++) { newArr[i + 1] = arr[i]; } foreach (int val in newArr) { Console.Write(val + " "); }
Attempts:
2 left
💡 Hint
Remember that for even length arrays, middle is length/2.
✗ Incorrect
The element 99 is inserted at index 3 (middle), shifting elements 40, 50, 60 right.
🔧 Debug
advancedIdentify the error in array insertion code
What error will this C# code produce when trying to insert an element in the middle of an array?
DSA C#
int[] arr = {1, 2, 3, 4}; int insertVal = 99; int mid = arr.Length / 2; int[] newArr = new int[arr.Length]; for (int i = 0; i < mid; i++) { newArr[i] = arr[i]; } newArr[mid] = insertVal; for (int i = mid; i < arr.Length; i++) { newArr[i + 1] = arr[i]; } foreach (int val in newArr) { Console.Write(val + " "); }
Attempts:
2 left
💡 Hint
Check the size of the new array and the indices used in the last loop.
✗ Incorrect
The new array size is same as original, but code tries to assign at index i+1 which can exceed bounds.
🧠 Conceptual
advancedTime complexity of inserting in middle of array
What is the time complexity of inserting an element in the middle of a fixed-size array by shifting elements?
Attempts:
2 left
💡 Hint
Consider how many elements need to move when inserting in the middle.
✗ Incorrect
Insertion requires shifting half the elements on average, so time is proportional to n.
🚀 Application
expertImplement middle insertion in a dynamic array
You have a dynamic array (List) in C#. Which code snippet correctly inserts an element in the middle without manual shifting?
Attempts:
2 left
💡 Hint
Look for a built-in method that inserts at a specific index.
✗ Incorrect
List.Insert inserts at index and shifts elements automatically.
