ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (checking if one string is a rotation of another)?

What will be the output of the following pseudocode (checking if one string is a rotation of another)?
Function isRotation(String a, String b)
    return length(a) == length(b) and contains(a + a, b)
End Function

Print isRotation("waterbottle", "erbottlewat"), isRotation("abc", "acb")
  1. A. true true
  2. B. false false
  3. C. true false
  4. D. false true

Correct answer: C. true false

Concatenating a string with itself contains every rotation of it as a substring: 'waterbottlewaterbottle' does contain 'erbottlewat', so the first is true. 'abcabc' does not contain 'acb' anywhere, so the second is false.

More Pseudocode and Code Tracing questions

Exams that ask this topic