Peek Top Element of Stack in DSA C - Time & Space Complexity
We want to understand how long it takes to look at the top item of a stack.
The question is: How does the time to peek change as the stack grows?
Analyze the time complexity of the following code snippet.
// Peek top element of stack
int peek(int stack[], int top) {
if (top == -1) {
return -1; // stack empty
}
return stack[top];
}
This code returns the top element of the stack without removing it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the element at the top index.
- How many times: Exactly once per peek call.
Looking at the top element takes the same time no matter how big the stack is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant as the stack size grows.
Time Complexity: O(1)
This means peeking the top element takes the same short time no matter how many items are in the stack.
[X] Wrong: "Peeking takes longer if the stack is bigger because it has more items to check."
[OK] Correct: Peeking only looks at one position, the top, so it does not depend on stack size.
Knowing that peek is a constant time operation helps you explain stack efficiency clearly and confidently.
"What if the stack was implemented as a linked list instead of an array? How would the time complexity of peek change?"
Practice
peek operation do in a stack?Solution
Step 1: Understand the peek operation
The peek operation looks at the top element of the stack without changing the stack.Step 2: Compare with other stack operations
Pop removes the top element, push adds an element, and isEmpty checks if stack is empty.Final Answer:
Returns the top element without removing it -> Option BQuick Check:
Peek = top element without removal [OK]
- Confusing peek with pop operation
- Thinking peek removes the element
- Mixing peek with push or isEmpty
stack in C if top is the index of the top element and arr is the array storing stack elements?Solution
Step 1: Understand stack top indexing
Top points to the current top element index in the array.Step 2: Access the top element
Accessing arr[top] gives the top element. Using top+1 or top-1 is incorrect as it points to wrong positions.Final Answer:
int x = arr[top]; -> Option DQuick Check:
Top index points to top element [OK]
- Using top+1 or top-1 instead of top
- Accessing arr[0] assuming stack bottom is top
- Confusing top index with element value
int stack[3] = {10, 20, 30};
int top = 2;
printf("%d", stack[top]);Solution
Step 1: Identify the top element index and stack contents
Stack array has elements 10 at index 0, 20 at 1, and 30 at 2. Top is 2.Step 2: Access and print the top element
stack[top] = stack[2] = 30, so printf prints 30.Final Answer:
30 -> Option CQuick Check:
stack[top] = 30 [OK]
- Confusing index 0 as top
- Assuming top is out of range
- Expecting pop removes element but code only prints
int stack[5] = {1, 2, 3, 4, 5};
int top = 4;
int peek() {
return stack[top + 1];
}Solution
Step 1: Analyze the peek function access
Peek returns stack[top + 1], which is stack[5] here (top=4), exceeding array size 5 (0-4).Step 2: Understand correct top indexing
Top points to current top element, so peek should return stack[top], not stack[top + 1].Final Answer:
Accessing stack[top + 1] causes out-of-bounds error -> Option AQuick Check:
Index out of bounds error [OK]
- Using top+1 instead of top for peek
- Ignoring array bounds causing runtime error
- Incorrect top initialization assumptions
stack[5] and a variable top initialized to -1, which sequence of operations correctly peeks the top element after pushing 3 elements (10, 20, 30)?Solution
Step 1: Understand push operation with top
Using stack[++top] = value increments top first, then assigns value, correctly pushing elements.Step 2: Peek top element after pushes
After pushing 3 elements, top points to last pushed element. Accessing stack[top] gives top element.Final Answer:
stack[++top] = 10; stack[++top] = 20; stack[++top] = 30; int x = stack[top]; -> Option AQuick Check:
Push increments top, peek uses stack[top] [OK]
- Using post-increment top++ causing wrong indexing
- Accessing stack[top + 1] for peek
- Not initializing top to -1 before push
Ready to prove it?
Pass the test and this pattern moves unknown
