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
- A. 5
- B. 10
- C. 3
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Using Booth-style thinking, the lexicographically smallest rotation of the string "banana" is wanted. The procedure below tries every…
- The word "mississippi" is examined by the procedure below, which counts how many different letters it contains. What is printed? seen =…
- A palindrome check on "racecar" uses two pointers from both ends, comparing one pair per step and stopping early on a mismatch. The…
- The string "abcabcbb" is scanned with a sliding window that keeps the longest stretch without a repeated character. The procedure below…
- Scanning "abcab dab" as a string of letters, the procedure below counts the adjacent pairs that read "ab" (both letters next to each…
- Two words are checked as anagrams by comparing letter counts. The procedure below sums the absolute differences between the letter counts…