ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (strings are indexed from 0)
What will be the output of the following pseudocode? (strings are indexed from 0)
String s = "{[()]}([)]"
Stack st = empty
Integer bad = -1
for (each i from 0 to length(s) - 1)
if (s[i] is an opening bracket)
push(st, s[i])
else if (st is empty OR top(st) does not match s[i])
bad = i
break
else
pop(st)
end if
end for
Print bad, size(st)- A. -1 0
- B. 9 2
- C. 8 2
- D. 7 1
Correct answer: C. 8 2
{[()]} is balanced and empties the stack. Then ( and [ are pushed, and the ) at index 8 does not match the [ on top, so bad = 8 with 2 brackets still on the stack: 8 2.
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? Queue q = {1, 2, 3, 4, 5} Integer k = 3 Stack st = empty for (each i from 1 to k)…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) // numbers stored as digit…
- What will be the output of the following pseudocode? // circular list of 6 people: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> back to 1 Node p = node 1…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What is the time complexity of the following pseudocode, as a function of n? Procedure g(Integer n) if (n <= 1) return for (each i from 1…
- What will be the output of the following pseudocode? Integer m[3][3] = {{11, 2, 4}, {4, 5, 6}, {10, 8, -12}} Integer d1 = 0, d2 = 0 for…