What if you could see the top of your stack without touching it at all?
Why Peek Top Element of Stack in DSA Typescript?
Imagine you have a stack of books on your desk. You want to see the title of the book on the very top without removing it. You try to pick up each book one by one to check, but that takes time and might mess up the order.
Manually checking the top book means moving books around, which is slow and can cause mistakes like dropping or mixing the order. It's hard to quickly see the top without disturbing the stack.
The peek operation lets you look at the top item of the stack instantly without removing it. It's like having a magic window to see the top book's title without touching the stack.
if (stack.length > 0) { const topBook = stack[stack.length - 1]; console.log(topBook); }
function peek(stack: string[]): string | undefined {
return stack[stack.length - 1];
}
console.log(peek(stack));Peek makes it easy and safe to check the top item anytime without changing the stack's order.
When undoing actions in a text editor, peek helps to see the last action without removing it, so you can decide if you want to undo it or not.
Manual checking of the top is slow and risky.
Peek lets you see the top item instantly without removal.
This keeps the stack safe and easy to use.
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
