ProgrammingHardAccenture · Cognizant

Run-length encoding replaces each run of equal characters by the character followed by its run length. The procedure below encodes s = "aaabccdddd" and prints the length of the encoded string. What is printed?

Run-length encoding replaces each run of equal characters by the character followed by its run length. The procedure below encodes s = "aaabccdddd" and prints the length of the encoded string. What is printed?
out = empty; i = 0
while i < n
    count the run starting at i (length k); append char and k
    i = i + k
end while
Print length of out
  1. A. 8
  2. B. 10
  3. C. 4
  4. D. 7

Correct answer: A. 8

The encoding is a3b1c2d4, which has 8 characters. The original string has length 10, the distinct letter count is 4, and omitting the count of 1 gives 7.

More Pseudocode and Code Tracing questions

Exams that ask this topic