Which of the following operations can be performed in O(1) time on a stack?

Practice Questions

Q1
Which of the following operations can be performed in O(1) time on a stack?
  1. Push
  2. Pop
  3. Peek
  4. All of the above

Questions & Step-by-Step Solutions

Which of the following operations can be performed in O(1) time on a stack?
  • Step 1: Understand what a stack is. A stack is a data structure that follows the Last In, First Out (LIFO) principle, meaning the last item added is the first one to be removed.
  • Step 2: Identify the basic operations of a stack. The main 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. O(1) time means that the operation takes a constant amount of time, regardless of the size of the stack.
  • Step 4: Analyze the 'push' operation. When you add an item to the top of the stack, it takes the same amount of time no matter how many items are already in the stack. Therefore, 'push' is O(1).
  • Step 5: Analyze the 'pop' operation. When you remove the top item from the stack, it also takes the same amount of time regardless of the stack size. Thus, 'pop' is O(1).
  • Step 6: Analyze the 'peek' operation. When you look at the top item without removing it, it takes a constant amount of time. So, 'peek' is O(1).
  • Step 7: Conclude that all three operations (push, pop, and peek) can be performed in O(1) time on a stack.
No concepts available.
Soulshift Feedback ×

On a scale of 0–10, how likely are you to recommend The Soulshift Academy?

Not likely Very likely