Consider a stack implemented using a linked list. The following operations are performed in order:
- Push 10
- Push 20
- Push 30
- Pop
- Push 40
What is the printed state of the stack from top to bottom?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class Stack { top: Node | null = null; push(value: number) { const newNode = new Node(value); newNode.next = this.top; this.top = newNode; } pop(): number | null { if (!this.top) return null; const poppedValue = this.top.value; this.top = this.top.next; return poppedValue; } printStack(): string { let current = this.top; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const stack = new Stack(); stack.push(10); stack.push(20); stack.push(30); stack.pop(); stack.push(40); console.log(stack.printStack());
Remember that pop removes the top element, and push adds a new top.
After pushing 10, 20, 30, the stack is 30 -> 20 -> 10 -> null.
Pop removes 30, so stack becomes 20 -> 10 -> null.
Push 40 adds 40 on top, so stack is 40 -> 20 -> 10 -> null.
Consider a stack implemented using a linked list. What is the time complexity of the push and pop operations?
Think about how many nodes you need to traverse for push and pop.
Both push and pop only change the top pointer and possibly one node's next pointer, so both are constant time O(1).
Given the following pop method in a stack implemented with a linked list, what error will occur if pop is called on an empty stack?
pop(): number {
const poppedValue = this.top.value;
this.top = this.top.next;
return poppedValue;
}What happens if this.top is null and you try to access this.top.value?
If the stack is empty, this.top is null. Accessing this.top.value causes a TypeError because null has no property 'value'.
Given the following sequence of operations on an empty stack implemented with a linked list:
- Push 5
- Push 15
- Pop
- Push 25
- Push 35
- Pop
- Pop
What is the printed stack state from top to bottom?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class Stack { top: Node | null = null; push(value: number) { const newNode = new Node(value); newNode.next = this.top; this.top = newNode; } pop(): number | null { if (!this.top) return null; const poppedValue = this.top.value; this.top = this.top.next; return poppedValue; } printStack(): string { let current = this.top; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const stack = new Stack(); stack.push(5); stack.push(15); stack.pop(); stack.push(25); stack.push(35); stack.pop(); stack.pop(); console.log(stack.printStack());
Track each push and pop carefully from top to bottom.
Push 5: stack is 5 -> null
Push 15: 15 -> 5 -> null
Pop removes 15: 5 -> null
Push 25: 25 -> 5 -> null
Push 35: 35 -> 25 -> 5 -> null
Pop removes 35: 25 -> 5 -> null
Pop removes 25: 5 -> null
Which of the following is the best reason to choose a linked list over an array when implementing a stack?
Think about memory allocation and resizing costs.
Linked lists grow dynamically without needing to resize or copy elements, unlike arrays which may need costly resizing operations.
