ProgrammingHardTCS NQT · Accenture
The procedure below counts the positions i in "mississippi" where the two letters s[i]s[i+1] are both "s" (overlaps allowed, though "ss" cannot overlap itself). What is printed?
The procedure below counts the positions i in "mississippi" where the two letters s[i]s[i+1] are both "s" (overlaps allowed, though "ss" cannot overlap itself). What is printed?
count = 0
for i = 0 to n - 2
if s[i] == 's' and s[i+1] == 's' then count = count + 1
end for
Print count- A. 2
- B. 0
- C. 1
- D. 4
Correct answer: A. 2
The double s appears at positions 2-3 and 5-6, so the count is 2. The double p gives 1, a double i does not occur at all, and the number of letters i is 4.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of…
- 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…