Question 8905 – ISRO CS 2020
December 26, 2023Question 72 – ISRO-2018
December 26, 2023Question 8907 – ISRO CS 2020
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()
pospos+1
Return A[pos]
end pop
Which of the following will initialize an empty stack with capacity N for the above implementation?
Correct Answer: D
Question 36 Explanation:
If you check push function, for every push we are decrementing pos by one.
Now if you imagine an array of length N-1, for every push the empty indices are getting decremented.
To explain in detail,
consider an empty array, so number of empty indices are N-1 ……(1)
When we push one element into it, the empty indices will reduce by 1 (i.e. decreented.)
Hence, From equation 1 we can say that pos is initialised to N-1
Now if you imagine an array of length N-1, for every push the empty indices are getting decremented.
To explain in detail,
consider an empty array, so number of empty indices are N-1 ……(1)
When we push one element into it, the empty indices will reduce by 1 (i.e. decreented.)
Hence, From equation 1 we can say that pos is initialised to N-1
pos ← -1
pos ← 0
pos ← 1
pos ←N-1
Subscribe
Login
0 Comments