Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the top element from the stack.
DSA Python
def pop(stack): if not stack: return None return stack[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append() which adds an element instead of removing.
Using remove() which removes by value, not by position.
✗ Incorrect
The pop() method removes and returns the last item from the list, which is the top of the stack.
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA Python
def safe_pop(stack): if len(stack) [1] 0: return stack.pop() return None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which only checks if stack is exactly zero.
Using '<' which checks for less than zero, impossible for length.
✗ Incorrect
We check if the stack length is greater than 0 before popping to avoid errors.
3fill in blank
hardFix the error in the pop function to correctly remove the top element.
DSA Python
def pop_element(stack): if stack: return stack[1]() return None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop(0) which removes the bottom element.
Using remove() which removes by value, not position.
✗ Incorrect
Using pop() without arguments removes the last element (top of stack). pop(0) removes the first element, which is incorrect for stack.
4fill in blank
hardFill both blanks to create a function that pops the top element and returns the updated stack.
DSA Python
def pop_and_return(stack): if stack: stack[1]() return [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of the updated stack.
Using append() which adds elements instead of removing.
✗ Incorrect
Calling pop() removes the top element. Returning stack shows the updated stack.
5fill in blank
hardFill all three blanks to pop the top element only if the stack is not empty and return the popped element.
DSA Python
def conditional_pop(stack): if len(stack) [1] 0: return stack[2]() else: return [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' to check stack length.
Returning stack instead of None when empty.
✗ Incorrect
Check if stack length is greater than 0, then pop the top element. Otherwise, return None.