ProgrammingHardAccenture · Cognizant
Strings are run-length compressed. The procedure below counts the number of runs in "aaabbc" (a run is a maximal block of equal consecutive characters). What is printed?
Strings are run-length compressed. The procedure below counts the number of runs in "aaabbc" (a run is a maximal block of equal consecutive characters). What is printed?
runs = 0; prev = none
for each character ch
if ch != prev then runs = runs + 1; prev = ch
end for
Print runs- A. 6
- B. 2
- C. 3
- D. 5
Correct answer: C. 3
The runs are aaa, bb and c, so there are 3 runs. Counting the changes between runs gives 2, the length is 6, and the adjacent pairs number 5.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below checks the bracket string "(()())" and prints the maximum nesting depth it reaches. The depth is raised by '(' and…
- The sentence "to be or not to be to" is split into words, and the procedure below prints the most frequent word (ties go to the first word…
- The string "aabbc" is examined by the procedure below, which counts the substrings (contiguous pieces, counted by position) that contain…
- The procedure below counts the positions i in "abbab" where the two-letter window s[i]s[i+1] is "ab" or "ba". What is printed? count = 0…
- 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"…
- The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of…