Data Structures & AlgorithmsModerateTCS NQT · string round
Problem: What does this run-length encoder print for 'aaabccdd'?
Problem: What does this run-length encoder print for 'aaabccdd'?
s = 'aaabccdd'
out = []
i = 0
while i < len(s):
j = i
while j < len(s) and s[j] == s[i]:
j += 1
out.append(s[i] + str(j - i))
i = j
print(''.join(out))What is printed?- A. a3b1c2d
- B. a3bc2d2
- C. a3b1c2d2
- D. aaabccdd
Correct answer: C. a3b1c2d2
Runs are a x3, b x1, c x2 and d x2, and each run is written as its letter followed by its length. Printing the input unchanged is the trap for a bad encoder.
More Coding Interview Problems questions
- Problem: Check bracket balance for '([]{})' and '([)]' with a stack. What is printed? python def ok(s): pairs = {')': '(', ']': '[', '}'…
- Problem: Sum the values of the next greater element of each position in [2, 1, 4, 3], where a position with no greater element contributes…
- Problem: Using at most one flip of a 0 to 1, what is the longest run of consecutive 1s in [1, 0, 1, 1, 0, 1]? Run the sliding-window code…
- Problem: How many unordered pairs of words in ['abc', 'bca', 'xyz', 'cab'] are anagrams of each other? Run the code. python from itertools…
- Problem: How many subsets of [1, 2, 3, 4, 5] have a sum of exactly 7? Run the code. python from itertools import combinations a = [1, 2…
- Problem: What is the length of the longest arithmetic subsequence of [1, 3, 5, 7, 4]? Run the O(n^2) DP code. python a = [1, 3, 5, 7, 4] n…