ProgrammingModerateAmazon OA · TCS NQT
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
// list: 10 -> 20 -> 30 -> 40 -> 50 -> NULL
Node fast = head, slow = head
Integer k = 2
for (each i from 1 to k) fast = fast.next
while (fast != NULL)
fast = fast.next
slow = slow.next
end while
Print slow.value- A. 40
- B. 30
- C. 20
- D. 50
Correct answer: A. 40
fast starts k = 2 nodes ahead, so when it falls off the end, slow is 2 nodes from the end: 40.
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 a[7] = {10, 5, 3, 4, 3, 5, 6} Set seen = empty Integer first = -1 for (each i…
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) for (each j from 1 to n) if (i == j)…
- Strings are immutable here, so s = s + "a" copies the whole current string. What is the time complexity of the following pseudocode, as a…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Boolean prime[2..50] //…
- What will be the output of the following pseudocode? Integer count = 0, n = 2, s = 0 while (count < 10) Boolean pr = true for (each d from…
- What will be the output of the following pseudocode? (XOR is true when exactly one side is true; NOT binds tighter than AND, which binds…