Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a new element at the beginning of the array.
DSA C#
int[] arr = {1, 2, 3, 4};
int newElement = 0;
int[] newArr = new int[arr.Length + 1];
newArr[0] = [1];
for (int i = 0; i < arr.Length; i++) {
newArr[i + 1] = arr[i];
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning arr[0] instead of the new element.
Using an index out of range.
Not increasing the array size.
✗ Incorrect
To insert at the beginning, assign the new element to the first position of the new array.
2fill in blank
mediumComplete the code to shift elements right to make space at the beginning.
DSA C#
int[] arr = {5, 6, 7};
int[] newArr = new int[arr.Length + 1];
for (int i = arr.Length - 1; i >= 0; i--) {
newArr[i + 1] = [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using newArr[i] instead of arr[i].
Using wrong index like i+1 on arr.
Looping in wrong direction.
✗ Incorrect
We copy each element from the original array to the next index in the new array to shift right.
3fill in blank
hardFix the error in the code to correctly insert at the beginning.
DSA C#
int[] arr = {10, 20, 30};
int newElement = 5;
int[] newArr = new int[arr.Length + 1];
for (int i = 0; i < arr.Length; i++) {
newArr[i + 1] = arr[i];
}
newArr[0] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning arr[0] instead of newElement.
Using an invalid index like arr[arr.Length].
Assigning to newArr[1] instead of newArr[0].
✗ Incorrect
The new element must be assigned to index 0 after the loop shifts elements.
4fill in blank
hardFill both blanks to create a new array with the element inserted at the beginning.
DSA C#
int[] arr = {3, 4, 5};
int newElement = 2;
int[] newArr = new int[arr.Length + 1];
newArr[[1]] = newElement;
for (int i = 0; i < arr.Length; i++) {
newArr[[2]] = arr[i];
}Attempts:
3 left
💡 Hint
Common Mistakes
Putting new element at wrong index.
Not shifting elements correctly.
Using i instead of i+1 for newArr index.
✗ Incorrect
The new element goes at index 0, and existing elements start from index 1 in the new array.
5fill in blank
hardFill all three blanks to insert a new element at the beginning and print the new array.
DSA C#
int[] arr = {7, 8, 9};
int newElement = 6;
int[] newArr = new int[arr.Length + 1];
newArr[[1]] = newElement;
for (int i = 0; i < arr.Length; i++) {
newArr[[2]] = arr[i];
}
for (int i = 0; i < newArr.Length; i++) {
Console.Write(newArr[[3]] + " ");
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for insertion or shifting.
Printing newElement instead of array elements.
Off-by-one errors in loops.
✗ Incorrect
Insert newElement at index 0, shift original elements starting at index 1, and print elements by index i.
