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- A. a1b0c2d3
- B. 2a1b3c4d
- C. abcd
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below scans the word "leetcode" from the left and prints the index of the first letter that occurs exactly once in the word…
- The procedure below counts the integers n with 1 <= n <= 60 that are not divisible by 2, 3 or 5 (coprime to 30). What is printed? count =…
- The procedure below counts the pairs i < j in the array [3, 1, 4, 1, 5] whose sum is even. What is printed? count = 0 for i = 0 to n - 2…
- The procedure below collects x mod 5 for every x from 1 to 20 into a set and prints the size of the set. What is printed? seen = empty set…
- Array a = [1, 1, 1, 1] (0-based). From index i you may jump forward by at most a[i] positions. The greedy below counts the minimum number…
- The procedure below sums the decimal digits of 2^10 = 1024 once and prints the result. What is printed? x = 2^10; total = 0 while x > 0…