What does the peek operation return when the stack is empty?
AThe bottom element of the stack
BAn error or indication that stack is empty
CThe first element pushed onto the stack
DThe entire stack
✗ Incorrect
Peeking an empty stack should return an error or a special value indicating the stack is empty.
In Go, how do you access the top element of a stack implemented as a slice named 'stack'?
Astack[len(stack)-1]
Bstack[-1]
Cstack[len(stack)]
Dstack[0]
✗ Incorrect
The top element is the last element in the slice, accessed by stack[len(stack)-1].
What is the time complexity of the peek operation on a stack?
AO(n)
BO(n^2)
CO(log n)
DO(1)
✗ Incorrect
Peek operation is O(1) because it directly accesses the top element.
Which of these is NOT true about the peek operation?
AIt requires checking if the stack is empty first
BIt shows the top element without removing it
CIt removes the top element from the stack
DIt runs in constant time
✗ Incorrect
Peek does not remove the top element; it only shows it.
What should you do before peeking to avoid runtime errors?
ACheck if the stack is empty
BClear the stack
CPop the top element
DPush a new element
✗ Incorrect
Always check if the stack is empty before peeking to avoid errors.
Explain how to safely peek the top element of a stack implemented as a slice in Go.
Think about slice length and indexing.
You got /3 concepts.
Describe the difference between peek and pop operations on a stack.
Focus on whether the element stays or is removed.
You got /3 concepts.
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 from the stack
C. Adds a new element to the top of the stack
D. Checks if the stack is empty
Solution
Step 1: Understand stack operations
A stack stores elements in a last-in, first-out order. Common operations include push (add), pop (remove), and peek (view top).
Step 2: Define peek operation
Peek returns the top element without removing it, allowing you to see the current top without changing the stack.
Final Answer:
Returns the top element without removing it -> Option A
Quick Check:
Peek = View 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 Go syntax to peek the top element of a stack implemented as a slice named stack?
easy
A. top := stack[len(stack)-1]
B. top := stack[-1]
C. top := stack[0]
D. top := stack[len(stack)]
Solution
Step 1: Understand Go slice indexing
Go slices are zero-indexed. The last element is at index length-1.
Step 2: Identify correct top element access
To peek top, access stack[len(stack)-1]. Using len(stack) is out of range, 0 is bottom, and negative index is invalid.
Final Answer:
top := stack[len(stack)-1] -> Option A
Quick Check:
Last element index = length-1 [OK]
Hint: Use len(stack)-1 to get top index [OK]
Common Mistakes:
Using len(stack) as index (out of range)
Using negative index (not supported in Go)
Accessing stack[0] which is bottom
3. What will be the output of the following Go code snippet?
stack := []int{10, 20, 30}
top := stack[len(stack)-1]
fmt.Println(top)
medium
A. 10
B. Runtime error
C. 20
D. 30
Solution
Step 1: Analyze stack contents
The stack slice contains elements [10, 20, 30], where 30 is the last element (top).
Step 2: Evaluate peek operation
top := stack[len(stack)-1] accesses stack[2], which is 30, then prints it.
Final Answer:
30 -> Option D
Quick Check:
Top element = 30 [OK]
Hint: Last element is top, printed value is 30 [OK]
Common Mistakes:
Printing bottom element instead of top
Indexing out of range causing error
Confusing output with pop operation
4. Identify the error in the following Go code that tries to peek the top element of a stack:
stack := []int{}
top := stack[len(stack)-1]
fmt.Println(top)
medium
A. Syntax error due to missing import
B. Index out of range runtime error
C. top will be zero by default
D. Code runs fine and prints 0
Solution
Step 1: Check stack initialization
The stack is initialized as an empty slice with length 0.
Step 2: Analyze peek operation
Accessing stack[len(stack)-1] means stack[-1], which is invalid and causes runtime panic: index out of range.
Final Answer:
Index out of range runtime error -> Option B
Quick Check:
Empty slice peek causes runtime error [OK]
Hint: Empty stack peek causes index error [OK]
Common Mistakes:
Assuming peek returns zero on empty stack
Ignoring runtime panic possibility
Confusing syntax error with runtime error
5. Given a stack implemented as a slice stack := []int{5, 10, 15}, write the correct Go code snippet to safely peek the top element and handle the case when the stack is empty.
hard
A. top := stack[0]
fmt.Println(top)
B. top := stack[len(stack)]
fmt.Println(top)
C. if len(stack) > 0 {
top := stack[len(stack)-1]
fmt.Println(top)
} else {
fmt.Println("Stack is empty")
}
D. if stack != nil {
top := stack[len(stack)-1]
fmt.Println(top)
}
Solution
Step 1: Check stack length before peeking
To avoid runtime error, verify stack length > 0 before accessing top element.
Step 2: Access top element safely and handle empty case
If stack is not empty, peek top with stack[len(stack)-1]. Otherwise, print "Stack is empty" message.
Final Answer:
if len(stack) > 0 {
top := stack[len(stack)-1]
fmt.Println(top)
} else {
fmt.Println("Stack is empty")
} -> Option C
Quick Check:
Check length before peek to avoid errors [OK]
Hint: Check length > 0 before peek [OK]
Common Mistakes:
Not checking length before peek
Using len(stack) as index
Assuming nil check is enough
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.