Data Structures & AlgorithmsHardICPC regional · string round
Problem: What is the length of the longest substring of 'banana' that occurs at least twice (occurrences may overlap)? Run the code.
Problem: What is the length of the longest substring of 'banana' that occurs at least twice (occurrences may overlap)? Run the code.
s = 'banana'
best = 0
for L in range(1, len(s)):
subs = [s[i:i + L] for i in range(len(s) - L + 1)]
if any(subs.count(x) >= 2 for x in subs):
best = L
print(best)What is printed?- A. 3
- B. 2
- C. 4
- D. 1
Correct answer: A. 3
'ana' occurs at indices 1 and 3 (overlapping at the shared a), and no length-4 substring repeats. Non-overlapping counting would give a different answer.
More Coding Interview Problems questions
- Problem: Subset sums of items [1, 2, 3] are computed with a bitset: reach starts as 1 and each item x does reach |= reach << x. How many…
- Problem: Maximum number of events attended when each event is [start, end] (day-inclusive) and you can attend one event per day. Events…
- Problem: How many decimal digits does 100! have? Run the code. python import math print(len(str(math.factorial(100)))) What is printed?
- Problem: What is the digit sum of 100!? Run the code. python import math print(sum(int(c) for c in str(math.factorial(100)))) What is…
- Problem: Longest path in a weighted DAG from vertex 0 with edges 0->1 (3), 1->2 (4), 0->2 (10). Run the DP over the topological order…
- Problem: How many ordered sequences of jumps of sizes 2, 3 or 5 sum exactly to 12? Run the DP. python ways = [1] + [0] 12 for v in…