ProgrammingVery hardTCS NQT · Accenture

Strings are compressed in place by writing each run of equal characters as the character followed by the run length (a count of 1 is omitted). The procedure below prints the length of the compressed result for the list ["a","a","b","b","c"]. What is printed?

Strings are compressed in place by writing each run of equal characters as the character followed by the run length (a count of 1 is omitted). The procedure below prints the length of the compressed result for the list ["a","a","b","b","c"]. What is printed?
compress the run lengths; result length = number of characters written
Print length
  1. A. 5
  2. B. 10
  3. C. 3
  4. D. 6

Correct answer: A. 5

The compressed form a2b2c has 5 characters, since the single c keeps no count. Counting the runs gives 3, writing the count for the single c gives 6, and doubling the output gives 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic