Codepath

Finding the Longest Winning Streak

TIP102 Unit 7 Session 1 Standard (Click for link to problem statements)

Problem 7: Finding the Longest Winning Streak

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).

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 20-25 mins
  • 🛠️ Topics: Recursion, String Processing

1: U-nderstand

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?
  • Q: What is the main task in this problem?
    • A: The task is to find the length of the longest consecutive streak of S in the string challenges using recursion.
  • Q: Can the string be empty?
    • A: Yes, an empty string should return 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`.

2: M-atch

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:

  • Recursive Streak Counting: Recursively traverse the string, counting the current streak of S characters and updating the maximum streak as you go.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea:

  • Use recursion to traverse the string while keeping track of the current streak length and the maximum streak length. If the current character is 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

  • Not resetting the current_length when encountering a non-S character.
  • Failing to correctly track and update the maximum streak length.

4: I-mplement

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)

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Trace through the longest_streak function with the input "SSOSSS". The function should return 3 after processing the entire string.
  • Test the function with edge cases like an empty string " or a string with no S characters. The function should return 0 for both cases.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(N) where N is the length of the string. The function processes each character exactly once.
  • Space Complexity: O(N) due to the recursion stack. The depth of recursion is proportional to the length of the string.
Fork me on GitHub