ProgrammingHardInfosys · Cognizant

The string "aabcccdddd" is compressed by the procedure below, which replaces each run of equal letters by the letter followed by the run length, and then prints the result. What is printed?

The string "aabcccdddd" is compressed by the procedure below, which replaces each run of equal letters by the letter followed by the run length, and then prints the result. What is printed?
out = empty; i = 0
while i < n
    k = length of the run starting at i
    append character and k to out; i = i + k
end while
Print out
  1. A. a1b0c2d3
  2. B. 2a1b3c4d
  3. C. abcd
  4. D. a2b1c3d4

Correct answer: D. a2b1c3d4

The runs are aa, b, ccc and dddd, so the output is a2b1c3d4. Subtracting one from each count, writing the count before the letter, and dropping the counts give different strings.

More Pseudocode and Code Tracing questions

Exams that ask this topic