Skip to content
Related Articles
Open in App
Not now

Related Articles

ISRO | ISRO CS 2020 | Question 10

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 03 Sep, 2020
Improve Article
Save Article
Like Article

A stack is implemented with an array of ‘A[0…N – 1]’ and a variable ‘pos’. The push and pop operations are defined by the following code.

push (x)
  A[pos] ← x
  pos ← pos – 1
end push

pop ( )
  pos ← pos + 1
  return A[pos]
end pop 

Which of the following will initialize an empty stack with capacity N for the above implementation ?
(A) pos ← –1
(B) pos ← 0
(C) pos ← 1
(D) pos ← N – 1

Answer: (D)

Explanation: Since we are using an array as a stack, we have a choice between 0 or N-1. Looking at the code, Pop increment pos and push decrement pos.

Stack is growing from larger index to lower index. Thus, for empty stack pos ← N – 1 is correct.

Quiz of this Question

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!