TIP102 Unit 7 Session 1 Standard (Click for link to problem statements)
In the kingdom's grand tournament, knights compete in a series of challenges. A knight's performance in the challenge is represented by a string challenges
, where a success is represented by an S
and each other outcome (like a draw or loss) is represented by an O
. Write a recursive function to find the length of the longest consecutive streak of successful challenges (S
).
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
S
in the string challenges
using recursion.0
as there are no streaks.HAPPY CASE
Input: "SSOSSS"
Output: 3
Explanation: The longest streak of consecutive `S` is three, found at the end of the string.
Input: "SOSOSOSO"
Output: 1
Explanation: The longest streak of consecutive `S` is only one.
EDGE CASE
Input: "
Output: 0
Explanation: An empty string has no challenges, so the longest streak is `0`.
Input: "OOOOOO"
Output: 0
Explanation: There are no successful challenges, so the longest streak is `0`.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Finding the Longest Streak, we want to consider the following approaches:
S
characters and updating the maximum streak as you go.Plan the solution with appropriate visualizations and pseudocode.
General Idea:
S
, increase the current streak; otherwise, reset it to 0
.Recursive Approach:
1) Base case: If the string `challenges` is empty, return the maximum streak length found so far (`max_length`).
2) If the current character is `S`, increment `current_length` and update `max_length` if `current_length` exceeds it.
3) If the current character is not `S`, reset `current_length` to `0`.
4) Recurse with the remaining string and the updated streak lengths.
⚠️ Common Mistakes
current_length
when encountering a non-S
character.Implement the code to solve the algorithm.
def longest_streak(challenges, current_length=0, max_length=0):
# Base case: If there are no more challenges to process
if not challenges:
return max_length
# If the current challenge is a success
if challenges[0] == "S":
current_length += 1
max_length = max(max_length, current_length)
else:
current_length = 0
# Recurse with the remaining challenges
return longest_streak(challenges[1:], current_length, max_length)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
longest_streak
function with the input "SSOSSS"
. The function should return 3
after processing the entire string."
or a string with no S
characters. The function should return 0
for both cases.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
where N
is the length of the string. The function processes each character exactly once.O(N)
due to the recursion stack. The depth of recursion is proportional to the length of the string.