ProgrammingHardInfosys · Cognizant
Using Booth-style thinking, the lexicographically smallest rotation of the string "banana" is wanted. The procedure below tries every rotation and prints the smallest. What is printed?
Using Booth-style thinking, the lexicographically smallest rotation of the string "banana" is wanted. The procedure below tries every rotation and prints the smallest. What is printed?
for k = 0 to n - 1
rotation = s[k..n-1] + s[0..k-1]
end for
Print the minimum rotation- A. nanaba
- B. abanan
- C. anaban
- D. banana
Correct answer: B. abanan
The rotations are banana, ananab, nanaba, anaban, nabana and abanan, and the smallest is abanan. The largest rotation is the mirror mistake, the second smallest is anaban, and printing the original word ignores rotation.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The word "mississippi" is examined by the procedure below, which counts how many different letters it contains. What is printed? seen =…
- A palindrome check on "racecar" uses two pointers from both ends, comparing one pair per step and stopping early on a mismatch. The…
- The string "abcabcbb" is scanned with a sliding window that keeps the longest stretch without a repeated character. The procedure below…
- Scanning "abcab dab" as a string of letters, the procedure below counts the adjacent pairs that read "ab" (both letters next to each…
- Two words are checked as anagrams by comparing letter counts. The procedure below sums the absolute differences between the letter counts…
- The minimum number of insertions or deletions needed to make the string "abcca" a palindrome is len(s) - LPS(s), where LPS is the length…