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.