ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer g[4][4] = {{0,0,0,0}, {0,1,0,0}, {0,0,0,1}, {1,0,0,0}} // 1 = blocked
Integer w[4][4] = all 0
for (each i from 0 to 3)
for (each j from 0 to 3)
if (g[i][j] == 1)
w[i][j] = 0
else if (i == 0 AND j == 0)
w[i][j] = 1
else
w[i][j] = (i > 0 ? w[i - 1][j] : 0) + (j > 0 ? w[i][j - 1] : 0)
end if
end for
end for
Print w[3][3]- A. 20
- B. 3
- C. 6
- D. 4
Correct answer: B. 3
w[i][j] counts right/down paths from the top-left that avoid blocked cells. Row by row: {1, 1, 1, 1}, {1, 0, 1, 2}, {1, 1, 2, 0}, {0, 1, 3, 3}, so w[3][3] = 3. With no blocked cells it would be C(6, 3) = 20.
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? (substring(s, i, len) is len characters starting at index i; a Set keeps one copy of…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; strings are indexed from 0)…
- What will be the output of the following pseudocode? Function p(Integer n, Integer k) if (n == 0) return 1 end if if (n < 0 OR k == 0)…
- What will be the output of the following pseudocode? Integer calls = 0 Function f(Integer a, Integer b) calls = calls + 1 if (b == 0)…
- What will be the output of the following pseudocode? Integer g[5][5] = {{1,1,0,0,0}, {1,0,0,1,1}, {0,0,1,0,0}, {1,0,1,1,0}, {1,0,0,0,1}}…
- What will be the output of the following pseudocode? Integer m[4][4], s = 0 for (each i from 0 to 3) for (each j from 0 to 3) m[i][j] = 4…