The 'peek' operation returns the top element of the stack without removing it, allowing you to see what is on top.
Click to reveal answer
beginner
In a stack, what happens if you try to peek when the stack is empty?
Peeking an empty stack usually returns a special value like null or undefined, or throws an error, because there is no top element to show.
Click to reveal answer
intermediate
TypeScript code snippet: How do you define a peek method for a stack class?
A peek method returns the last element in the internal array without removing it, for example:
peek(): T | undefined {
return this.items[this.items.length - 1];
}
Click to reveal answer
beginner
Why is peek useful compared to pop in stack operations?
Peek lets you see the top element without changing the stack, while pop removes the top element. Peek is useful when you want to check the top without losing data.
Click to reveal answer
beginner
What is the time complexity of the peek operation in a stack implemented with an array?
The peek operation runs in O(1) time because it directly accesses the last element of the array without looping.
Click to reveal answer
What does the peek operation return in a stack?
AThe bottom element
BRemoves and returns the top element
CThe top element without removing it
DThe size of the stack
✗ Incorrect
Peek returns the top element without removing it, unlike pop which removes it.
If a stack is empty, what should peek return or do?
AReturn the top element
BReturn null or undefined, or throw an error
CAdd a new element
DRemove the bottom element
✗ Incorrect
Peeking an empty stack has no top element, so it returns null/undefined or throws an error.
What is the time complexity of peek in a stack implemented with an array?
AO(1)
BO(log n)
CO(n)
DO(n^2)
✗ Incorrect
Peek accesses the last element directly, so it runs in constant time O(1).
Which stack operation removes the top element?
APop
BPush
CPeek and Pop
DPeek
✗ Incorrect
Pop removes and returns the top element, peek only returns it without removing.
In TypeScript, how do you access the top element of a stack stored as an array named 'items'?
Aitems.pop()
Bitems[0]
Citems.length
Ditems[items.length - 1]
✗ Incorrect
The top element is the last element in the array, accessed by items[items.length - 1].
Explain how the peek operation works in a stack and why it is useful.
Think about checking the top book on a stack without taking it off.
You got /3 concepts.
Write a simple TypeScript peek method for a stack implemented with an array.
Use array indexing to get the last element.
You got /3 concepts.
Practice
(1/5)
1. What does the peek operation do in a stack?
easy
A. Returns the top element without removing it
B. Removes the top element and returns it
C. Adds a new element to the top
D. Checks if the stack is empty
Solution
Step 1: Understand stack operations
A stack supports push (add), pop (remove), and peek (view) operations.
Step 2: Define peek operation
Peek returns the top element without removing it, unlike pop which removes it.
Final Answer:
Returns the top element without removing it -> Option A
Quick Check:
Peek = view top element only [OK]
Hint: Peek shows top without removing it [OK]
Common Mistakes:
Confusing peek with pop
Thinking peek adds elements
Assuming peek checks emptiness
2. Which of the following is the correct TypeScript syntax to peek the top element of a stack implemented as an array named stack?
easy
A. stack[stack.length - 1]
B. stack.peek()
C. stack.pop()
D. stack.top()
Solution
Step 1: Recall array indexing for top element
The top element of a stack implemented as an array is at index length - 1.
Step 2: Identify correct syntax
Accessing stack[stack.length - 1] returns the top element without removing it.
Final Answer:
stack[stack.length - 1] -> Option A
Quick Check:
Array top = stack[length - 1] [OK]
Hint: Use stack[length - 1] to peek top [OK]
Common Mistakes:
Using pop() which removes element
Using non-existent peek() method on array
Using top() which is not a standard method
3. What will be the output of the following TypeScript code?
D. Using pop() removes the top element instead of just peeking
Solution
Step 1: Understand pop() behavior
pop() removes and returns the top element, modifying the stack.
Step 2: Identify peek operation requirement
Peek should return the top element without removing it, so pop() is incorrect here.
Final Answer:
Using pop() removes the top element instead of just peeking -> Option D
Quick Check:
pop() removes element, peek should not [OK]
Hint: Peek must not remove element; pop() does [OK]
Common Mistakes:
Using pop() instead of accessing last element
Returning stack[0] which is bottom element
Ignoring side effects of pop()
5. You have a stack implemented as an array stack. You want to safely peek the top element but return null if the stack is empty to avoid errors. Which TypeScript code snippet correctly does this?
hard
A. return stack.pop() || null;
B. return stack.length ? stack[stack.length - 1] : null;
C. return stack[0] || null;
D. return stack[stack.length];
Solution
Step 1: Check if stack is empty
Use stack.length to verify if stack has elements.
Step 2: Return top element or null
If stack is not empty, return stack[stack.length - 1], else return null.