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
  1. A. 6
  2. B. 2
  3. C. 3
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic