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, head points to node 1
Node prev = NULL, cur = head, nxt
Integer k = 0
while (cur != NULL AND k < 3)
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
k = k + 1
end while
head.next = cur
head = prev
Print the values from head to NULL- A. 3 2 1
- B. 5 4 3 2 1
- C. 1 4 5
- D. 3 2 1 4 5
Correct answer: D. 3 2 1 4 5
The loop reverses the first three nodes (3 → 2 → 1). The old head (node 1) is now last in that block and is reconnected to node 4, so the list reads 3 2 1 4 5.
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? (a loop whose start is greater than its end runs zero times) Integer count = 0…
- What will be the output of the following pseudocode? Integer stars = 0 Procedure p(Integer n) stars = stars + 1 for (each i from 1 to n)…
- What will be the output of the following pseudocode? (& is bitwise AND on two's-complement integers) Integer n = 360 while (n > 0) Print n…
- What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; indices start at 0)…
- What will be the output of the following pseudocode? Function F(Integer n) if (n == 0) return 1 end if return n - M(F(n - 1)) End Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {1, 2, 3, -2, 2, 1}, c = 0, s for (each i…