Peek Top Element of Stack in DSA Typescript - Time & Space Complexity
We want to understand how long it takes to look at the top item of a stack.
How does the time needed change when the stack grows bigger?
Analyze the time complexity of the following code snippet.
class Stack {
private items: number[] = [];
peek(): number | undefined {
return this.items[this.items.length - 1];
}
}
This code returns the top element of the stack without removing it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the last element of an array.
- How many times: Exactly once per peek call.
Looking at the top element takes the same time no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even if the stack grows.
Time Complexity: O(1)
This means peeking the top element takes a constant amount of time regardless of stack size.
[X] Wrong: "Peeking takes longer if the stack is bigger because it has to check many elements."
[OK] Correct: Peeking only looks at the last element directly, so it does not depend on stack size.
Knowing that peek is a quick, constant-time operation helps you explain stack behavior clearly in interviews.
"What if the stack was implemented using a linked list instead of an array? How would the time complexity of peek change?"
Practice
peek operation do in a stack?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 AQuick Check:
Peek = view top element only [OK]
- Confusing peek with pop
- Thinking peek adds elements
- Assuming peek checks emptiness
stack?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
Accessingstack[stack.length - 1]returns the top element without removing it.Final Answer:
stack[stack.length - 1] -> Option AQuick Check:
Array top = stack[length - 1] [OK]
- Using pop() which removes element
- Using non-existent peek() method on array
- Using top() which is not a standard method
const stack: number[] = []; stack.push(10); stack.push(20); stack.push(30); console.log(stack[stack.length - 1]);
Solution
Step 1: Trace stack push operations
Elements pushed in order: 10, then 20, then 30. Stack is [10, 20, 30].Step 2: Access top element using stack[stack.length - 1]
stack.length is 3, so index 2 is top element which is 30.Final Answer:
30 -> Option CQuick Check:
Top element after pushes = 30 [OK]
- Confusing first pushed element as top
- Using pop() instead of peek
- Off-by-one error in index
const stack: number[] = [5, 15, 25];
function peek() {
return stack.pop();
}
console.log(peek());
console.log(stack);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 DQuick Check:
pop() removes element, peek should not [OK]
- Using pop() instead of accessing last element
- Returning stack[0] which is bottom element
- Ignoring side effects of pop()
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?Solution
Step 1: Check if stack is empty
Usestack.lengthto verify if stack has elements.Step 2: Return top element or null
If stack is not empty, returnstack[stack.length - 1], else returnnull.Final Answer:
return stack.length ? stack[stack.length - 1] : null; -> Option BQuick Check:
Safe peek with empty check = return stack.length ? stack[stack.length - 1] : null; [OK]
- Using pop() which removes element
- Accessing stack[0] which is bottom element
- Accessing stack[stack.length] which is undefined
Ready to prove it?
Pass the test and this pattern moves unknown
