ProgrammingHardTCS NQT · Infosys · Capgemini pattern
What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack?
What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack?
push 89 pop push 55 push 85 pop push 91 push 81 push 77 pop pop
- A. 55, 91, 81
- B. 81, 77
- C. 55, 91
- D. 91, 55
Correct answer: C. 55, 91
Tracing push/pop on an empty stack in order gives, bottom-to-top, [55, 91]. Reading top-to-bottom instead gives the reversed trap [91, 55]; mistakenly treating "pop" as removing the front (as in a queue) gives [81, 77].
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?…
- What is the tightest time complexity of the following pseudocode, in terms of n? (Treat any constant like 4 as a fixed constant, not…
- Consider this naive recursive Fibonacci. How many TOTAL calls to fib (including the initial call) happen when computing fib(6)? Function…
- What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array…
- What will be the output of the following pseudocode? (^ is bitwise XOR.) Integer x = 90, y = 76 x = x ^ y y = x ^ y x = x ^ y Print x, y
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…