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?
- A. 6
- B. 4
- C. 3
- 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
- Problem: Pancake sort uses only prefix reversals on [3, 1, 2]. For each size from n down to 2, bring the largest value of the unsorted…
- Problem: In the array [2, 7, 11, 15, 3, 8], how many pairs i < j have a[i] + a[j] equal to a multiple of 5?
- Problem: The binary search for a value in the sorted array [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] is run with the standard midpoint mid =…
- Problem: The bit string 1011001 is read as a binary number. What is its value when each bit is XOR-ed with the previous bit (the first bit…
- Problem: How many monotone right/down paths go from (0,0) to (3,3) on a 4 x 4 grid when cell (1,2) is blocked? Run the DP. python g = [[0]…
- Problem: How many twin prime pairs (p, p + 2), both prime, have p + 2 below 10000? Run the sieve. python N = 10000 s = [True] (N + 1) s[0]…