ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
String s = "ABBACA"
Stack st = empty
for (each ch in s)
if (st not empty AND top(st) == ch)
pop(st)
else
push(st, ch)
end if
end for
Print stack contents from bottom to top- A. AACA
- B. CA
- C. ACA
- D. BBCA
Correct answer: B. CA
Equal neighbours cancel, and cancellations can chain: B B cancel, which brings A next to A, which also cancel. What is left is CA.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 3, 4, 5, 6} Integer i = 0, j = 5 while (i < j) if (a[i] mod 2…
- What will be the output of the following pseudocode? Integer a[5] = {0, 1, 0, 3, 12}, k = 0 for (each i from 0 to 4) if (a[i] != 0)…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {-1, 0, 1, 2, -1, -4} sort a // {-4, -1…
- What will be the output of the following pseudocode? Integer a[3] = {5, -3, 5} Integer total = 0, curMax = 0, bestMax = a[0], curMin = 0…
- What will be the output of the following pseudocode? Integer n = 5, spaces = 0, stars = 0 for (each i from 1 to n) for (each j from 1 to n…
- What will be the output of the following pseudocode? (~ is bitwise NOT, & is AND, | is OR, >> is right shift) Integer x = 11 Integer a =…