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 an element at the end of the array?
Consider the following C# code that inserts an element at the end of an array by creating a new array with increased size. What will be printed after insertion?
DSA C#
int[] InsertAtEnd(int[] arr, int value) { int[] newArr = new int[arr.Length + 1]; for (int i = 0; i < arr.Length; i++) { newArr[i] = arr[i]; } newArr[newArr.Length - 1] = value; return newArr; } int[] original = {1, 2, 3}; int[] result = InsertAtEnd(original, 4); for (int i = 0; i < result.Length; i++) { Console.Write(result[i] + " -> "); } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Think about how the new array copies old elements and adds the new value at the last position.
✗ Incorrect
The function creates a new array one element larger, copies all old elements, then places the new value at the last index. So the output shows the original elements followed by the new value 4.
❓ Predict Output
intermediateWhat error occurs when inserting at end without resizing the array?
What happens if you try to insert an element at the end of a fixed-size array in C# without creating a new array?
DSA C#
int[] arr = {1, 2, 3}; arr[arr.Length] = 4; Console.WriteLine(string.Join(", ", arr));
Attempts:
2 left
💡 Hint
Arrays have fixed size; accessing index equal to length is invalid.
✗ Incorrect
Arrays in C# have fixed size. Trying to access arr[arr.Length] is out of bounds and throws IndexOutOfRangeException.
🔧 Debug
advancedFind the bug in this array insertion code
This code tries to insert a value at the end of an array but produces wrong output. What is the bug?
DSA C#
int[] InsertAtEnd(int[] arr, int value) { int[] newArr = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) { newArr[i] = arr[i]; } newArr[newArr.Length - 1] = value; return newArr; } int[] original = {1, 2, 3}; int[] result = InsertAtEnd(original, 4); for (int i = 0; i < result.Length; i++) { Console.Write(result[i] + " -> "); } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Check the size of the new array compared to the original.
✗ Incorrect
The new array has the same length as original, so assigning value at last index overwrites the last original element instead of adding a new one.
🧠 Conceptual
advancedWhy is inserting at the end of an array costly?
Inserting an element at the end of a fixed-size array requires creating a new array and copying elements. Why is this operation costly compared to linked lists?
Attempts:
2 left
💡 Hint
Think about what happens internally when you add an element beyond the array's fixed size.
✗ Incorrect
Arrays have fixed size. To add an element, a new larger array must be created and all elements copied, which takes time proportional to the number of elements.
❓ Predict Output
expertWhat is the output after multiple insertions at the end?
Given the code below that inserts multiple elements at the end of an array, what is the final printed output?
DSA C#
int[] InsertAtEnd(int[] arr, int value) { int[] newArr = new int[arr.Length + 1]; for (int i = 0; i < arr.Length; i++) { newArr[i] = arr[i]; } newArr[newArr.Length - 1] = value; return newArr; } int[] arr = {1}; arr = InsertAtEnd(arr, 2); arr = InsertAtEnd(arr, 3); arr = InsertAtEnd(arr, 4); for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + " -> "); } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Each insertion creates a new array with one more element, preserving previous elements in order.
✗ Incorrect
Each call adds one element at the end, so after three insertions starting from {1}, the array becomes {1, 2, 3, 4}.
