ISRO Scientist/Engineer CSE 2020Data Structures › Stacks and Expression Evaluation
ISRO Scientist/Engineer CSE 2020 PYQ: A stack is implemented with an array of $’A[0...N-1]’$ and a variable ‘$pos$’. The push and pop operations…
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 \leftarrow -1$
- B$pos\leftarrow 0$
- C$pos\leftarrow 1$
- D$pos\leftarrow N-1$Correct answer
Answer
(D) $pos\leftarrow N-1$
Approach
Analyze the given push and pop operations to determine the direction in which the stack grows. The push operation writes to A[pos] and then decrements pos, meaning the stack fills from higher indices to lower indices. To maximize capacity, the first push must occur at the highest valid index, which is N-1.
Reviewed by ISRO IQ Editorial Team.