Question: Which of the following operations has a time complexity of O(1) in a stack?
Options:
Push
Pop
Peek
All of the above
Correct Answer: All of the above
Solution:
All operations (Push, Pop, and Peek) in a stack are performed in constant time, O(1).
Which of the following operations has a time complexity of O(1) in a stack?
Practice Questions
Q1
Which of the following operations has a time complexity of O(1) in a stack?
Push
Pop
Peek
All of the above
Questions & Step-by-Step Solutions
Which of the following operations has a time complexity of O(1) in a stack?
Step 1: Understand what a stack is. A stack is a data structure that follows the Last In First Out (LIFO) principle.
Step 2: Identify the main operations of a stack: Push (adding an item), Pop (removing the top item), and Peek (viewing the top item without removing it).
Step 3: Learn about time complexity. Time complexity describes how the time to complete an operation grows with the size of the input.
Step 4: Recognize that in a stack, all three operations (Push, Pop, and Peek) can be done without needing to look at or change other items in the stack.
Step 5: Conclude that since these operations do not depend on the number of items in the stack, they all have a time complexity of O(1), meaning they take constant time.
No concepts available.
Soulshift Feedback×
On a scale of 0–10, how likely are you to recommend
The Soulshift Academy?