Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The string "aabcccccaaa" is run-length encoded (each run becomes its letter followed by its run length, e.g. "a2b1..."). What is the length of the encoded string?
- A. 6
- B. 7
- C. 11
- D. 8
Correct answer: D. 8
The runs are aa, b, ccccc, aaa, encoded as a2 b1 c5 a3 = 8 characters. Merging equal letters across runs (a5 b1 c5) gives 6, dropping the count for single letters (a2 b c5 a3) gives 7, and 11 is the original length.
More Coding Interview Problems questions
- Problem: Find the length of the shortest substring of "xaybzcxyabc" that contains all the letters a, b and c at least once.
- Problem: A Conway's Game of Life grid on a 4×4 board with alive cells at (1,0), (1,1), (1,2), (0,2) is advanced by one generation…
- Problem: A list of 6 customers arrives at a bank counter with service times 4, 1, 3, 2, 6, 5 (in that arrival order). The counter serves…
- Problem: A linked list of 10 nodes is traversed with Floyd's tortoise and hare: the slow pointer moves 1 step and the fast pointer moves 2…
- Problem: An LRU cache holds 3 entries. Keys are accessed in this order: 1, 2, 3, 1, 4, 2, 5, 1, 3, 4. A key is a hit if it is already…
- Problem: Tower of Hanoi with 6 disks, moving all disks from peg A to peg C using peg B as the spare (standard rules). How many moves does…