Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the top node from the stack and return its value.
DSA Java
public int pop() {
if (top == null) return -1;
int value = top.data;
top = top.[1];
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors because the stack is singly linked.
Returning the wrong node's data.
✗ Incorrect
The 'next' pointer of the top node points to the next node in the stack. Updating top to top.next removes the current top.
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA Java
public int pop() {
if (top == [1]) return -1;
int value = top.data;
top = top.next;
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking top == 0 is incorrect because top is a node, not a number.
Using 'empty' or 'false' which are not valid node references.
✗ Incorrect
The stack is empty if top is null, so we check if top == null before popping.
3fill in blank
hardFix the error in the pop method to correctly update the top pointer.
DSA Java
public int pop() {
if (top == null) return -1;
int value = top.data;
top = [1];
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using top.prev causes null pointer exceptions because it's singly linked.
Using unrelated pointers like child or parent.
✗ Incorrect
The top pointer should move to the next node in the stack, which is top.next.
4fill in blank
hardFill both blanks to pop the top node and update the stack size.
DSA Java
public int pop() {
if (top == null) return -1;
int value = top.data;
top = top.[1];
size[2];
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Increasing size after popping is incorrect.
Using wrong pointer names like prev.
✗ Incorrect
We update top to top.next to remove the node, and decrease size by 1 using size--.
5fill in blank
hardFill all three blanks to pop the top node, update size, and handle empty stack.
DSA Java
public int pop() {
if (top == [1]) return -1;
int value = top.[2];
top = top.[3];
size--;
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which does not exist in singly linked list.
Not checking for null before popping.
✗ Incorrect
Check if top is null to detect empty stack, get value from top.data, and update top to top.next.
