Data Structures & AlgorithmsModerateTCS NQT · Product Company OA

Problem: How many distinct non-empty prefixes does the string 'banana' have? Run the code.

Problem: How many distinct non-empty prefixes does the string 'banana' have? Run the code.
s = 'banana'
print(len({s[:i] for i in range(1, len(s) + 1)}))
What is printed?
  1. A. 5
  2. B. 6
  3. C. 4
  4. D. 3

Correct answer: B. 6

The prefixes are b, ba, ban, bana, banan and banana, so 6 distinct prefixes exist.

More Coding Interview Problems questions

Exams that ask this topic