ProgrammingVery hardTCS NQT · Accenture
The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of length 3 (windows start at 0 to n-3). What is printed?
The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of length 3 (windows start at 0 to n-3). What is printed?
count = 0
for i = 0 to n - 3
if s[i] == s[i+2] then count = count + 1
end for
Print count- A. 0
- B. 3
- C. 5
- D. 7
Correct answer: B. 3
The windows aba, bac, aca, cab and aba are checked: the first, third and last are palindromes, so the count is 3. Testing adjacent equal letters finds none, the window count is 5, and the length of the word is 7.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The number of distinct substrings (non-empty, counted once per distinct text) of the string "aab" is computed by the procedure below using…
- Two positions in the word "hello" are chosen (the letters at the two positions form a pair, and positions are distinct). The procedure…
- The procedure below counts the cyclic rotations of "abab" that are equal to "abab" itself, checking the rotations obtained by moving k…
- Coin denominations are 1, 4 and 6. The greedy method below pays amount 8 by always taking the largest coin that still fits (coins are…
- Ropes of lengths 1, 2, 3, 4 and 5 are merged two at a time with cost equal to the sum of the two lengths. The greedy merges the two…
- A truck carries at most 10 boxes. The box types are given as (units per box, number of boxes): (5, 3), (4, 2), (3, 4), (2, 5). The greedy…