0
0
DSA Pythonprogramming~10 mins

Stack Implementation Using Array in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add an element to the top of the stack.

DSA Python
def push(stack, element):
    stack.[1](element)
Drag options to blanks, or click blank then click option'
Ainsert
Bremove
Cappend
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of append removes an element instead of adding one.
Using remove tries to delete a specific element, not add one.
2fill in blank
medium

Complete the code to remove and return the top element from the stack.

DSA Python
def pop(stack):
    if len(stack) == 0:
        return None
    return stack.[1]()
Drag options to blanks, or click blank then click option'
Apop
Bappend
Cremove
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using append adds an element instead of removing one.
Using remove requires a value and does not return the removed element.
3fill in blank
hard

Fix the error in the code to check if the stack is empty.

DSA Python
def is_empty(stack):
    return len(stack) [1] 0
Drag options to blanks, or click blank then click option'
A!=
B==
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' checks if stack is not empty.
Using '>' or '<' does not correctly check for emptiness.
4fill in blank
hard

Fill both blanks to peek at the top element without removing it.

DSA Python
def peek(stack):
    if len(stack) == 0:
        return None
    return stack[1][2]
Drag options to blanks, or click blank then click option'
A[0]
B[-2]
C()
D[-1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses tries to call the list as a function.
Using index 0 accesses the bottom, not the top.
5fill in blank
hard

Fill all three blanks to implement a function that returns the size of the stack and clears it.

DSA Python
def clear_and_size(stack):
    size = len(stack)
    stack[1]()
    return [2], [3]
Drag options to blanks, or click blank then click option'
Aclear
Bsize
Clen(stack)
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes only one element, not all.
Returning variables in wrong order or forgetting to save size.