Updated 18 days ago | GitHub

Reversing a Scroll

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

Problem 3: Reversing a Scroll

A wizard is deciphering an ancient scroll and needs to reverse the letters in a word to reveal a hidden message. Write a recursive function to reverse the letters in a given scroll and return the reversed scroll. Assume scroll only contains alphabetic characters.

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Recursion, String Manipulation

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 reverse a string using a recursive function.
  • Q: Can the string be empty?
    • A: Yes, if the string is empty, the function should return an empty string.
HAPPY CASE
Input: "cigam"
Output: "magic"
Explanation: The string "cigam" reversed is "magic".

Input: "lleps"
Output: "spell"
Explanation: The string "lleps" reversed is "spell".

EDGE CASE
Input: ""
Output: ""
Explanation: An empty string should return an empty string.

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 Reversing a String, we want to consider the following approaches:

  • Recursive Reversal: Recursively reduce the string by one character, reversing the rest and appending the first character at the end.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea:

  • Reverse the string by taking the first character, calling the function recursively on the rest of the string, and then appending the first character to the result of the recursive call.

Recursive Approach:

1) Base case: If the string `scroll` is empty, return the empty string.
2) Recursive case: 
   a) Call the function recursively on the substring `scroll[1:]`.
   b) Append the first character `scroll[0]` to the result of the recursive call.
   c) Return the reversed string.

⚠️ Common Mistakes

  • Not handling the base case correctly, which can lead to infinite recursion.
  • Incorrectly managing string slicing, which could result in errors or missed characters.

4: I-mplement

Implement the code to solve the algorithm.

def reverse_scroll(scroll):
    if len(scroll) == 0:
        return scroll
    return reverse_scroll(scroll[1:]) + scroll[0]

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 reverse_scroll function with the input "cigam". The function should return "magic" after recursively reversing each letter.
  • Test the function with edge cases like an empty string "". The function should return "", correctly handling the base case.

6: E-valuate

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

  • Time Complexity: O(N^2) where N is the length of the string. There are N recursive calls, and each evaluates scroll[1:], which allocates a new string in O(N) time per call (Python string slicing is O(k) for a slice of length k). The trailing + scroll[0] concatenation is also O(k) and contributes another O(N^2) term.
  • Space Complexity: O(N^2) in the worst case. The recursion reaches depth N, and each frame retains a sliced suffix of sizes N, N-1, ..., 1, so the total memory held across the stack is O(N^2).