ProgrammingModerateCAT · MBA placement
Count the palindromic substrings (substrings of length 1 or more that read the same forwards and backwards, counted by position) of the string "aaa". The procedure below counts them by checking every substring. What is printed?
Count the palindromic substrings (substrings of length 1 or more that read the same forwards and backwards, counted by position) of the string "aaa". The procedure below counts them by checking every substring. What is printed?
count = 0
for i = 0 to n - 1
for j = i to n - 1
if s[i..j] is a palindrome then count = count + 1
end for
end for
Print count- A. 3
- B. 2
- C. 8
- D. 6
Correct answer: D. 6
All six substrings are palindromes: three single letters, two copies of aa and the whole word aaa. Counting only single letters gives 3, counting only length-2 substrings gives 2, and the number of subsets 2^3 = 8 is a different count.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Run-length encoding replaces each run of equal characters by the character followed by its run length. The procedure below encodes s =…
- A Caesar cipher shifts each lowercase letter forward by 3 places, wrapping from z back to a. The procedure below encodes the word "xyz"…
- The number of distinct arrangements (permutations of the letters, with identical letters not counted twice) of the string "AABBC" is…
- The string "Programming Language" is taken in lowercase and the procedure below counts its vowels (a, e, i, o, u). What is printed? count…
- For the bracket string "(()(()))", the procedure below tracks the nesting depth and prints the maximum depth reached. What is printed?…
- Longest common prefix of the words "flower", "flow" and "flight" is found by comparing characters column by column until a mismatch. The…