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 -> 6 -> NULL
Node slow = head, fast = head
while (fast.next != NULL AND fast.next.next != NULL)
slow = slow.next
fast = fast.next.next
end while
Print slow.value- A. 3
- B. 4
- C. 6
- D. 2
Correct answer: A. 3
fast moves 1 → 3 → 5 and then stops because 5 has only one node after it; slow moves 1 → 2 → 3. For an even-length list this test gives the FIRST of the two middle nodes, 3. Testing fast != NULL AND fast.next != NULL instead gives 4.
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? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Integer j for (each d from 0 to…
- What will be the output of the following pseudocode? (strings are indexed from 0) Procedure perm(String s, Integer l) // s is passed by…
- What will be the output of the following pseudocode? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first) Integer…
- What will be the output of the following pseudocode? Integer n = 6, count = 0 Integer col[n] Function ok(Integer r, Integer c) for (each k…
- What will be the output of the following pseudocode? (i++ gives the old value of i and then adds 1) Integer i = 0, c = 0 while (i++ < 5) c…
- What will be the output of the following pseudocode? (--n subtracts 1 from n and then gives the new value) Integer n = 5, s = 0 do s = s +…