ProgrammingHardAmazon OA · Infosys SP

What will be the output of the following pseudocode? (substring(t, i, k) is the k characters starting at index i; indices start at 0)

What will be the output of the following pseudocode? (substring(t, i, k) is the k characters starting at index i; indices start at 0)
String s1 = "ABCDE", s2 = "DEABC"
String t = s1 + s1
Integer pos = -1
for (each i from 0 to length(t) - length(s2))
    if (substring(t, i, length(s2)) == s2)
        pos = i
        break
    end if
end for
Print t, pos
  1. A. ABCDEABCDE 2
  2. B. ABCDEABCDE -1
  3. C. ABCDEABCDE 3
  4. D. ABCDEDEABC 5

Correct answer: C. ABCDEABCDE 3

s2 is a rotation of s1 exactly when it appears inside s1 + s1 = ABCDEABCDE. DEABC starts at index 3: ABC|DEABC|DE. Output ABCDEABCDE 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic