ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer m[4][4] = {{4, 3, 2, -1}, {3, 2, 1, -1}, {1, 1, -1, -2}, {-1, -1, -2, -3}}
Integer r = 0, c = 3, neg = 0
while (r < 4 AND c >= 0)
if (m[r][c] < 0)
neg = neg + (4 - r)
c = c - 1
else
r = r + 1
end if
end while
Print neg- A. 6
- B. 7
- C. 16
- D. 8
Correct answer: D. 8
Rows and columns are sorted in decreasing order, so once m[r][c] is negative, everything below it in column c is negative too. Columns 3, 2, 1, 0 contribute 4, 2, 1, 1: neg = 8.
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? // list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Integer n = 5, k = 2 Node tail = last node…
- What will be the output of the following pseudocode? Insert into an empty BST, in this order: 50, 30, 70, 20, 40, 60, 80, 35, 45, 65…
- What will be the output of the following pseudocode? // undirected graph; neighbours are visited in increasing order Edges: 1-2, 1-3, 2-4…
- What will be the output of the following pseudocode? // undirected graph; neighbours are visited in increasing order Edges: 1-2, 1-3, 2-4…
- What will be the output of the following pseudocode? Integer parent[1..8] for (each i from 1 to 8) parent[i] = i Function find(Integer x)…
- What will be the output of the following pseudocode? // directed graph with edge weights Edges: A->B 4, A->C 1, C->B 2, B->D 1, C->D 5…