ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
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, D->E 3
dist[A] = 0, all others = infinity
repeat until every vertex is finalised
pick the unfinalised vertex u with the smallest dist and finalise it
for (each edge u->v with weight w)
if (dist[u] + w < dist[v]) dist[v] = dist[u] + w
end for
end repeat
Print dist[B], dist[D], dist[E]- A. 4 5 8
- B. 3 4 7
- C. 3 6 9
- D. 4 6 9
Correct answer: B. 3 4 7
Dijkstra: finalise A (0), then C (1), which improves B to 3 and D to 6; then B (3) improves D to 4; then D (4) gives E = 7. Output 3 4 7.
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? // directed edges: 5->2, 5->0, 4->0, 4->1, 2->3, 3->1 Compute indegree of every…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 0, digitsA = 0…
- What will be the output of the following pseudocode? Function odd(Integer n) if (n == 0) return 0 end if return odd(n - 1) + 2 n - 1 End…
- What will be the output of the following pseudocode? ((mask >> i) & 1 is evaluated before ==; ^ is XOR) Integer a[3] = {5, 1, 6}, total =…
- What will be the output of the following pseudocode? (+ joins strings) String s = "PAYPALISHIRING" Integer rows = 3, r = 0, step = 1…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {1, 3, 5, 4, 2}, n = 5 Integer i = n - 2…