QUESTION 18 Which one of the given options is correct about stacks, implemented using arrays?
Micron technical mcq question, verified with a worked answer. Free to practise - no sign-up.
QUESTION 18
Which one of the given options is correct about stacks, implemented using arrays?
Show answer & explanation
Stack operations (push, pop, peek) implemented using arrays are all O(1) constant time operations because they only involve accessing or modifying the top element via an index pointer. Option B is incorrect because arrays have fixed size and don't grow/shrink easily without reallocation; option C is wrong because stack operations don't inherently require extra space; option D is false since arrays are a standard, efficient stack implementation.
Step-by-step Derivation:
Analysis of stack array implementation:
- Push Operation: Increment top pointer and insert element at arr[top] → O(1)
- Pop Operation: Retrieve element at arr[top] and decrement top → O(1)
- Peek Operation: Access element at arr[top] without modification → O(1)
- isEmpty/isFull: Check top pointer against boundaries → O(1)
All operations involve only index manipulation and array element access, both constant time. No loops or recursive calls are needed. Therefore, every core stack operation executes in O(1) time regardless of stack size.
Comparison of other options:
- B: Arrays have fixed size; dynamic growth requires costly reallocation/copying
- C: Stack operations don't require auxiliary data structures; space used is O(1) per operation
- D: Arrays are actually one of the most efficient ways to implement stacks