Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Peek Top Element of Stack in DSA Javascript - 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 in this stack?
Consider the following JavaScript code that implements a stack and peeks the top element. What will be printed?
DSA Javascript
class Stack {
  constructor() {
    this.items = [];
  }
  push(element) {
    this.items.push(element);
  }
  peek() {
    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
B30
C20
Dundefined
Attempts:
2 left
💡 Hint
Remember, peek returns the last pushed element without removing it.
❓ Predict Output
intermediate
2:00remaining
What happens when peeking an empty stack?
Given the stack implementation below, what will be the output of peeking when the stack is empty?
DSA Javascript
class Stack {
  constructor() {
    this.items = [];
  }
  peek() {
    return this.items[this.items.length - 1];
  }
}
const stack = new Stack();
console.log(stack.peek());
Aundefined
Bnull
C0
DThrows an error
Attempts:
2 left
💡 Hint
Accessing an array element at negative index returns what?
❓ Predict Output
advanced
2:00remaining
What is the output after multiple peek calls?
Look at the code below. What will be printed after the two peek calls?
DSA Javascript
class Stack {
  constructor() {
    this.items = [];
  }
  push(element) {
    this.items.push(element);
  }
  peek() {
    return this.items[this.items.length - 1];
  }
  pop() {
    return this.items.pop();
  }
}
const stack = new Stack();
stack.push(5);
stack.push(15);
console.log(stack.peek());
stack.pop();
console.log(stack.peek());
A5\n5
B5\n15
C15\n15
D15\n5
Attempts:
2 left
💡 Hint
Peek shows the top element without removing it; pop removes the top element.
❓ Predict Output
advanced
2:00remaining
What error occurs when peeking from a stack with no items and accessing length incorrectly?
What error will this code produce?
DSA Javascript
class Stack {
  constructor() {
    this.items = null;
  }
  peek() {
    return this.items[this.items.length - 1];
  }
}
const stack = new Stack();
console.log(stack.peek());
Aundefined
BReferenceError: items is not defined
CTypeError: Cannot read property 'length' of null
DSyntaxError
Attempts:
2 left
💡 Hint
Check what happens when you try to access a property of null.
🧠 Conceptual
expert
2:00remaining
Why is peek operation in a stack O(1) time complexity?
Choose the best explanation for why the peek operation on a stack runs in constant time.
ABecause peek accesses the last element directly using the array index without iteration.
BBecause peek searches the entire stack to find the top element.
CBecause peek removes all elements except the top one, which takes constant time.
DBecause peek copies all elements to a new array before returning the top.
Attempts:
2 left
💡 Hint
Think about how arrays allow direct access by index.

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 stores elements with last-in, first-out order. Peek shows the top element without removing it.
  2. Step 2: Differentiate peek from pop

    Pop removes and returns the top element, peek only returns it without removal.
  3. Final Answer:

    Returns the top element without removing it -> Option A
  4. Quick Check:

    Peek = top element without removal [OK]
Hint: Peek shows top without removing it [OK]
Common Mistakes:
  • Confusing peek with pop operation
  • Thinking peek adds elements
  • Assuming peek checks emptiness
2. Which of the following is the correct syntax to peek the top element of a stack implemented as an array named stack in JavaScript?
easy
A. stack.peek()
B. stack.pop()
C. stack[stack.length - 1]
D. stack.top()

Solution

  1. Step 1: Understand JavaScript array indexing

    JavaScript arrays use zero-based indexing. The last element is at index length - 1.
  2. Step 2: Identify correct peek syntax

    To peek, access the last element without removing it: stack[stack.length - 1]. stack.pop() removes the element, stack.peek() and stack.top() are not default methods.
  3. Final Answer:

    stack[stack.length - 1] -> Option C
  4. Quick Check:

    Peek = stack[stack.length - 1] [OK]
Hint: Use stack[stack.length - 1] to peek top [OK]
Common Mistakes:
  • Using stack.pop() which removes element
  • Using non-existent methods like peek() or top()
  • Accessing stack[stack.length] which is undefined
3. What will be the output of the following code?
const stack = [10, 20, 30];
const top = stack[stack.length - 1];
console.log(top);
medium
A. 10
B. 30
C. 20
D. undefined

Solution

  1. Step 1: Identify the stack elements and length

    The stack array is [10, 20, 30]. Its length is 3.
  2. Step 2: Calculate the top element index and value

    stack.length - 1 = 3 - 1 = 2. stack[2] is 30.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    Top element = 30 [OK]
Hint: Top element is last array item: stack[length-1] [OK]
Common Mistakes:
  • Using stack[stack.length] which is undefined
  • Confusing first element with top element
  • Printing stack.pop() instead of peek
4. Find the error in this code snippet that tries to peek the top element of a stack:
const stack = [5, 15, 25];
function peek() {
  return stack[stack.length];
}
console.log(peek());
medium
A. stack.length should be stack.length - 1
B. stack is not defined
C. peek function should remove the element
D. console.log is missing parentheses

Solution

  1. Step 1: Analyze the peek function index

    stack.length returns the number of elements, but array indices go from 0 to length - 1.
  2. Step 2: Correct the index to access top element

    Use stack.length - 1 to get the last element. Using stack.length returns undefined.
  3. Final Answer:

    stack.length should be stack.length - 1 -> Option A
  4. Quick Check:

    Index = length - 1 for top element [OK]
Hint: Use length - 1 to access last element [OK]
Common Mistakes:
  • Using stack.length as index causing undefined
  • Thinking peek removes element
  • Missing parentheses in console.log (not in this code)
5. Given a stack implemented as an array, how can you safely peek the top element without causing an error if the stack is empty?
function safePeek(stack) {
  // Your code here
}
hard
A. return stack[stack.length];
B. return stack[0];
C. return stack.pop();
D. return stack.length > 0 ? stack[stack.length - 1] : null;

Solution

  1. Step 1: Check if stack is empty

    Before accessing the top element, verify stack length is greater than zero to avoid undefined access.
  2. Step 2: Return top element or null safely

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

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

    Safe peek uses length check and conditional [OK]
Hint: Check length > 0 before peek to avoid errors [OK]
Common Mistakes:
  • Accessing stack[stack.length] causing undefined
  • Using pop which removes element
  • Returning stack[0] which is bottom, not top
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