ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (recursively reversing a list)?
What will be the output of the following pseudocode (recursively reversing a list)?
Function reverseList(List lst)
if (length(lst) <= 1)
return lst
end if
return reverseList(lst[1 .. end]) + [lst[0]]
End Function
Print reverseList({1, 2, 3, 4, 5})- A. [5, 4, 3, 2, 1]
- B. [1, 2, 3, 4, 5]
- C. [5, 1, 2, 3, 4]
- D. [1, 5, 4, 3, 2]
Correct answer: A. [5, 4, 3, 2, 1]
Each call peels off the first element and appends it AFTER the recursively reversed remainder, building the list in fully reversed order: [5, 4, 3, 2, 1].
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 (counting set bits for every number from 0 to n using a DP recurrence)? (>> is right…
- What will be the output of the following pseudocode (reversing the order of words in a sentence)? String s = "the sky is blue" List words…
- What will be the output of the following pseudocode (number of ways to make change for an amount using given coin denominations, counting…
- What will be the output of the following pseudocode (length of the longest common SUBSTRING, i.e. contiguous, not subsequence)? Function…
- What will be the output of the following pseudocode (counting distinct ordered character pairs (s[i], s[j]) with i<j)? (Strings are…
- What will be the output of the following pseudocode (capitalizing the first letter of each word, manually)? String s = "hello world foo"…