Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the stack using an array.
DSA C#
stack[top] = value; top[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using '+=' causes incorrect increment.
Using '-' decrements top, losing data.
✗ Incorrect
Place the value at the current top (next free position), then increment top.
2fill in blank
mediumComplete the code to pop an element from the stack using an array.
DSA C#
if(top > 0) { top[1]; return stack[top]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' causes out-of-bounds access.
Using '-=' with larger value skips elements.
✗ Incorrect
Decrement top first, then return the element now pointed by top.
3fill in blank
hardFix the error in the code that checks if the stack is empty.
DSA C#
bool isEmpty = (top [1] 0);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' is true when not empty.
Using '>' or '<' incorrectly identifies empty state.
✗ Incorrect
The stack is empty when top equals 0 (no elements added).
4fill in blank
hardFill both blanks to complete the stack push method with boundary check.
DSA C#
if(top [1] maxSize) { throw new Exception("Stack Overflow"); } stack[top] = value; top[2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' fails to detect overflow.
Using '--' after push removes the element.
✗ Incorrect
Check top >= maxSize for overflow, then push and increment top.
5fill in blank
hardFill the blanks to implement a safe pop method with underflow check.
DSA C#
if(top [1] 0) { throw new Exception("Stack Underflow"); } top[2]; return stack[top];
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' allows pop when top == 0.
Using '--' after return or wrong order.
✗ Incorrect
Check if top <= 0 for underflow, decrement top, return stack[top].
