Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A string is compressed by replacing each maximal run of equal letters with the letter and its count, only when the count is at least 2. For "aaabccdddd", the output is "a3bccd4". How many characters does the output have after compressing "aabbbbcccccc" this way?

  1. A. 6
  2. B. 4
  3. C. 3
  4. D. 12

Correct answer: A. 6

Runs are aa, bbbb, cccccc, so the output is "a2b4c6" with 6 characters. Forgetting to flush the final run after the loop gives "a2b4" (4), keeping only the letters gives 3, and 12 is the uncompressed length.

More Coding Interview Problems questions

Exams that ask this topic