0
0
DSA Pythonprogramming~10 mins

Stack vs Array Direct Use Why We Need Stack Abstraction in DSA Python - Interactive Comparison 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 stack using list append.

DSA Python
stack = []
stack.[1](5)
print(stack)
Drag options to blanks, or click blank then click option'
Apop
Binsert
Cappend
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop or remove instead of append.
Using insert which adds at a specific position.
2fill in blank
medium

Complete the code to remove the top element from the stack using list method.

DSA Python
stack = [1, 2, 3]
top = stack.[1]()
print(top, stack)
Drag options to blanks, or click blank then click option'
Apop
Bappend
Cremove
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove which deletes by value, not position.
Using append which adds instead of removing.
3fill in blank
hard

Fix the error in the code that tries to use list as a stack but incorrectly accesses the top element.

DSA Python
stack = [10, 20, 30]
top = stack[1]  # get top element
print(top)
Drag options to blanks, or click blank then click option'
A[0]
B[1]
C[-2]
D[-1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which is the bottom of the stack.
Using index 1 or -2 which are not the top element.
4fill in blank
hard

Fill both blanks to create a stack abstraction class with push and pop methods.

DSA Python
class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.[1](item)

    def pop(self):
        return self.items.[2]()
Drag options to blanks, or click blank then click option'
Aappend
Binsert
Cpop
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert instead of append for push.
Using remove instead of pop for pop method.
5fill in blank
hard

Fill in the blank to implement a method that checks if the stack is empty and returns a boolean.

DSA Python
class Stack:
    def __init__(self):
        self.items = []
    def push(self, item):
        self.items.append(item)

    def is_empty(self):
        return len(self.items) [1] 0

stack = Stack()
print(stack.is_empty())  # True
stack.push(1)
print(stack.is_empty())  # False
Drag options to blanks, or click blank then click option'
A==
B>
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < which check for non-empty incorrectly.
Using != which returns True for non-empty stacks.