Which of the following operations has a time complexity of O(n) in a stack?
Practice Questions
Q1
Which of the following operations has a time complexity of O(n) in a stack?
Push
Pop
Peek
None of the above
Questions & Step-by-Step Solutions
Which of the following operations has a time complexity of O(n) 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 basic operations of a stack. The basic operations are 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 is a way to describe how the time to complete an operation grows as the size of the input (n) increases.
Step 4: Analyze the time complexity of each basic operation in a stack. All basic operations (Push, Pop, Peek) can be done in constant time, which is O(1).
Step 5: Compare the time complexities. Since all basic operations are O(1), none of them have a time complexity of O(n).
Step 6: Conclude that the answer to the question is 'None of the above' because no operation has a time complexity of O(n).