ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
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
tail.next = head // make the list circular
Node p = head
for (each i from 1 to n - k - 1)
p = p.next
end for
head = p.next
p.next = NULL
Print the values from head to NULL- A. 3 4 5 1 2
- B. 5 4 3 2 1
- C. 2 3 4 5 1
- D. 4 5 1 2 3
Correct answer: D. 4 5 1 2 3
p stops at node 3 (n − k − 1 = 2 steps from node 1). The list is cut after it and the new head is node 4, giving a right rotation by k = 2: 4 5 1 2 3.
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? 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…
- 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…