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?- A. 5
- B. 6
- C. 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
- Problem: How many integers in [1, 30] are even but not divisible by 4? Run the code. python print(sum(1 for x in range(1, 31) if x % 2 ==…
- Problem: What is the total length covered by the union of the intervals [1, 3], [2, 6] and [8, 10]? Run the code. python iv = sorted([(1…
- Problem: What does this Python snippet print? def add(x, bucket=[]): bucket.append(x) return len(bucket) add(1); add(2) print(add(3))
- Problem: What does this Python snippet print? g = [[0]3]3 g[1][2] = 7 print(sum(map(sum, g)))
- Problem: What does this Python snippet print? print(-7 // 2, -7 % 3, 7 // -2)
- Problem: What does this snippet print? gen = (xx for x in range(4)) print(sum(gen), sum(gen))