ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (counting permutations of a string via recursion)?

What will be the output of the following pseudocode (counting permutations of a string via recursion)?
Function countPerms(String s)
    if (length(s) <= 1)
        return 1
    end if
    return length(s) * countPerms(substring(s, 1))
End Function

Print countPerms("abcd")
  1. A. 10
  2. B. 16
  3. C. 20
  4. D. 24

Correct answer: D. 24

This computes n! via recursion: length(s)=4 multiplied by countPerms of the remaining 3-character string, unwinding to 4*3*2*1 = 24.

More Pseudocode and Code Tracing questions

Exams that ask this topic