0
0
DSA Pythonprogramming~20 mins

Stack Implementation Using Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after these stack operations?

Consider a stack implemented using a linked list. The following operations are performed in order:

  1. push(10)
  2. push(20)
  3. pop()
  4. push(30)
  5. pop()
  6. push(40)

What is the printed state of the stack from top to bottom?

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Stack:
    def __init__(self):
        self.top = None

    def push(self, data):
        new_node = Node(data)
        new_node.next = self.top
        self.top = new_node

    def pop(self):
        if self.top is None:
            return None
        popped = self.top.data
        self.top = self.top.next
        return popped

    def print_stack(self):
        current = self.top
        result = []
        while current:
            result.append(str(current.data))
            current = current.next
        print(" -> ".join(result) + " -> null")

stack = Stack()
stack.push(10)
stack.push(20)
stack.pop()
stack.push(30)
stack.pop()
stack.push(40)
stack.print_stack()
A40 -> 10 -> null
B20 -> 10 -> null
C30 -> 10 -> null
D10 -> 40 -> null
Attempts:
2 left
💡 Hint

Remember that push adds to the top and pop removes from the top.

Predict Output
intermediate
1:30remaining
What error occurs when popping from an empty stack?

Given the stack implementation below, what happens if we call pop() on an empty stack?

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Stack:
    def __init__(self):
        self.top = None

    def pop(self):
        if self.top is None:
            return None
        popped = self.top.data
        self.top = self.top.next
        return popped

stack = Stack()
print(stack.pop())
ATypeError
BNone
CAttributeError
DIndexError
Attempts:
2 left
💡 Hint

Check the condition when the stack is empty before popping.

🔧 Debug
advanced
2:30remaining
Identify the bug in this push method

Look at the push method below. What is the bug that will cause incorrect stack behavior?

DSA Python
def push(self, data):
    new_node = Node(data)
    if self.top is None:
        self.top = new_node
    else:
        self.top.next = new_node
        self.top = new_node
AThe new node is added at the bottom instead of the top
BThe top pointer is not updated correctly
CThe next pointer of the new node is not set to the current top
DThe method does not create a new node
Attempts:
2 left
💡 Hint

Remember the linked list stack adds new nodes at the front.

🚀 Application
advanced
2:00remaining
How many elements remain after these operations?

Starting with an empty stack, perform these operations:

  1. push(5)
  2. push(15)
  3. pop()
  4. push(25)
  5. push(35)
  6. pop()
  7. pop()

How many elements remain in the stack?

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Stack:
    def __init__(self):
        self.top = None

    def push(self, data):
        new_node = Node(data)
        new_node.next = self.top
        self.top = new_node

    def pop(self):
        if self.top is None:
            return None
        popped = self.top.data
        self.top = self.top.next
        return popped

    def size(self):
        count = 0
        current = self.top
        while current:
            count += 1
            current = current.next
        return count

stack = Stack()
stack.push(5)
stack.push(15)
stack.pop()
stack.push(25)
stack.push(35)
stack.pop()
stack.pop()
print(stack.size())
A0
B2
C3
D1
Attempts:
2 left
💡 Hint

Count pushes and pops carefully.

🧠 Conceptual
expert
1:30remaining
Why is linked list preferred over array for stack in some cases?

Which reason best explains why a linked list implementation of a stack might be preferred over an array implementation?

ALinked list allows dynamic size without resizing overhead
BLinked list uses less memory than arrays always
CLinked list provides faster access to middle elements
DLinked list requires less code to implement than arrays
Attempts:
2 left
💡 Hint

Think about how arrays handle size changes.