Challenge - 5 Problems
Stack Peek Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of peeking the top element?
Consider the following TypeScript stack implementation and operations. What will be printed after peeking the top element?
DSA Typescript
class Stack { private items: number[] = []; push(element: number) { this.items.push(element); } pop(): number | undefined { return this.items.pop(); } peek(): number | undefined { return this.items[this.items.length - 1]; } } const stack = new Stack(); stack.push(10); stack.push(20); stack.push(30); console.log(stack.peek());
Attempts:
2 left
💡 Hint
Peek returns the last pushed element without removing it.
✗ Incorrect
The stack has elements [10, 20, 30]. The top is the last pushed element, 30. Peek returns 30 without removing it.
❓ Predict Output
intermediateWhat happens when peeking an empty stack?
What will be the output of peeking the top element of an empty stack in this TypeScript code?
DSA Typescript
class Stack { private items: number[] = []; peek(): number | undefined { return this.items[this.items.length - 1]; } } const stack = new Stack(); console.log(stack.peek());
Attempts:
2 left
💡 Hint
Accessing an array element at negative index returns undefined in JavaScript/TypeScript.
✗ Incorrect
The items array is empty, so items.length - 1 is -1. Accessing items[-1] returns undefined.
🔧 Debug
advancedWhy does this peek method return wrong value?
This peek method is intended to return the top element of the stack. Why does it return undefined after pushing elements?
DSA Typescript
class Stack { private items: number[] = []; push(element: number) { this.items.push(element); } peek(): number | undefined { return this.items[this.items.length]; } } const stack = new Stack(); stack.push(5); stack.push(15); console.log(stack.peek());
Attempts:
2 left
💡 Hint
Array indices start at 0, last element is at length - 1.
✗ Incorrect
The peek method accesses items[items.length], which is undefined because array indices go from 0 to length - 1.
🧠 Conceptual
advancedWhat is the time complexity of peek operation in a stack?
Given a stack implemented with an array, what is the time complexity of the peek operation?
Attempts:
2 left
💡 Hint
Peek accesses the last element directly without loops.
✗ Incorrect
Peek returns the last element by direct index access, which takes constant time.
❓ Predict Output
expertWhat is the output after multiple push, pop, and peek operations?
Given the following TypeScript stack code, what is printed by the last console.log?
DSA Typescript
class Stack { private items: number[] = []; push(element: number) { this.items.push(element); } pop(): number | undefined { return this.items.pop(); } peek(): number | undefined { return this.items[this.items.length - 1]; } } const stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); stack.pop(); stack.push(4); stack.pop(); console.log(stack.peek());
Attempts:
2 left
💡 Hint
Track the stack after each operation carefully.
✗ Incorrect
Stack changes: push 1 -> [1], push 2 -> [1,2], push 3 -> [1,2,3], pop -> [1,2], push 4 -> [1,2,4], pop -> [1,2]. Peek returns 2.
