Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Peek Top Element of Stack in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Peek Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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());
A10
Bundefined
C20
D30
Attempts:
2 left
💡 Hint
Peek returns the last pushed element without removing it.
❓ Predict Output
intermediate
2:00remaining
What 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());
Aundefined
B0
Cnull
DThrows an error
Attempts:
2 left
💡 Hint
Accessing an array element at negative index returns undefined in JavaScript/TypeScript.
🔧 Debug
advanced
2:30remaining
Why 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());
ABecause peek should remove the element before returning
BBecause items.length is out of array bounds, should use items.length - 1
CBecause push method does not add elements correctly
DBecause items array is empty
Attempts:
2 left
💡 Hint
Array indices start at 0, last element is at length - 1.
🧠 Conceptual
advanced
1:30remaining
What 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?
AO(1) - constant time
BO(n) - linear time
CO(log n) - logarithmic time
DO(n^2) - quadratic time
Attempts:
2 left
💡 Hint
Peek accesses the last element directly without loops.
❓ Predict Output
expert
3:00remaining
What 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());
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Track the stack after each operation carefully.

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

  1. Step 1: Understand stack operations

    A stack supports push (add), pop (remove), and peek (view) operations.
  2. Step 2: Define peek operation

    Peek returns the top element without removing it, unlike pop which removes it.
  3. Final Answer:

    Returns the top element without removing it -> Option A
  4. 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

  1. Step 1: Recall array indexing for top element

    The top element of a stack implemented as an array is at index length - 1.
  2. Step 2: Identify correct syntax

    Accessing stack[stack.length - 1] returns the top element without removing it.
  3. Final Answer:

    stack[stack.length - 1] -> Option A
  4. 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?
const stack: number[] = [];
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack[stack.length - 1]);
medium
A. 10
B. 20
C. 30
D. undefined

Solution

  1. Step 1: Trace stack push operations

    Elements pushed in order: 10, then 20, then 30. Stack is [10, 20, 30].
  2. Step 2: Access top element using stack[stack.length - 1]

    stack.length is 3, so index 2 is top element which is 30.
  3. Final Answer:

    30 -> Option C
  4. Quick Check:

    Top element after pushes = 30 [OK]
Hint: Last pushed element is top [OK]
Common Mistakes:
  • Confusing first pushed element as top
  • Using pop() instead of peek
  • Off-by-one error in index
4. Identify the error in the following TypeScript code that tries to peek the top element of a stack implemented as an array:
const stack: number[] = [5, 15, 25];
function peek() {
  return stack.pop();
}
console.log(peek());
console.log(stack);
medium
A. Missing return type in peek function
B. stack array is not initialized properly
C. peek function should return stack[0]
D. Using pop() removes the top element instead of just peeking

Solution

  1. Step 1: Understand pop() behavior

    pop() removes and returns the top element, modifying the stack.
  2. Step 2: Identify peek operation requirement

    Peek should return the top element without removing it, so pop() is incorrect here.
  3. Final Answer:

    Using pop() removes the top element instead of just peeking -> Option D
  4. 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

  1. Step 1: Check if stack is empty

    Use stack.length to verify if stack has elements.
  2. Step 2: Return top element or null

    If stack is not empty, return stack[stack.length - 1], else return null.
  3. Final Answer:

    return stack.length ? stack[stack.length - 1] : null; -> Option B
  4. Quick Check:

    Safe peek with empty check = return stack.length ? stack[stack.length - 1] : null; [OK]
Hint: Check length before peek to avoid errors [OK]
Common Mistakes:
  • Using pop() which removes element
  • Accessing stack[0] which is bottom element
  • Accessing stack[stack.length] which is undefined
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard