Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the array that stores stack elements.
DSA C#
private int[] [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared in the class.
Confusing the array name with the stack size variable.
✗ Incorrect
The array that stores the stack elements is named 'stackArray' in this implementation.
2fill in blank
mediumComplete the code to double the size of the array when resizing.
DSA C#
int newSize = stackArray.Length [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' which adds instead of doubling.
Using '/' which halves the size.
✗ Incorrect
To double the size, multiply the current length by 2.
3fill in blank
hardFix the error in the Push method to add the new element correctly.
DSA C#
stackArray[[1]] = item;Attempts:
3 left
💡 Hint
Common Mistakes
Using count - 1 which overwrites the last element.
Using count + 1 which skips an index.
✗ Incorrect
The new item should be placed at the index equal to the current count before incrementing.
4fill in blank
hardFill both blanks to correctly resize and copy elements to the new array.
DSA C#
int[] newArray = new int[[1]]; Array.[2](stackArray, newArray, count);
Attempts:
3 left
💡 Hint
Common Mistakes
Using CopyTo which is an instance method, not static.
Using Clone which creates a shallow copy but not to a new array.
✗ Incorrect
Create new array with newSize and use Array.Copy to copy elements.
5fill in blank
hardFill all three blanks to correctly implement the Pop method.
DSA C#
if (count == 0) throw new InvalidOperationException("Stack is empty"); count [1]; int item = stackArray[[2]]; stackArray[[3]] = 0; return item;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count - 1' instead of 'count' for the index.
Not decrementing count before accessing the array.
✗ Incorrect
Decrement count first, then get item at count, clear that index, and return the item.
