ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (strings are indexed from 0)
What will be the output of the following pseudocode? (strings are indexed from 0)
String s = "ABCABCAB"
Integer n = length(s), p = n
for (each k from 1 to n)
Boolean ok = true
for (each i from k to n - 1)
if (s[i] != s[i - k]) ok = false
end for
if (ok)
p = k
break
end if
end for
Print p- A. 8
- B. 6
- C. 2
- D. 3
Correct answer: D. 3
p is the smallest k such that every character equals the one k places before it, i.e. the string's smallest period. ABCABCAB repeats ABC, so p = 3, even though 3 does not divide its length 8.
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? (strings are indexed from 0; + joins strings) String s = "ABCDEF", r = "" Integer k =…
- What will be the output of the following pseudocode? Integer a[7] = {1, 3, 2, 4, 1, 5, 2}, c = 0 for (each i from 1 to 5) if (a[i] > a[i…
- What will be the output of the following pseudocode? Integer a[5] = {1, 2, 3, 4, 5} Integer odd = 0, even = 1, s = 0, ans = 0 // counts of…
- What will be the output of the following pseudocode? (& is bitwise AND on 32-bit integers) Function pow4(Integer n) return n > 0 AND (n &…
- What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift) Integer x = 7, m = 1 while ((x & m) != 0) x =…
- What will be the output of the following pseudocode? Integer printed = 0 Procedure perm(String s, Integer l) // s is passed by value if (l…