Given a string containing words separated by spaces, reverse the order of words in the string. For example, given the input string "Hello World", the output should be "World Hello".

Function Signature:

def reverse_words(s: str) -> str:
    """
    Reverse the order of words in the given string.

    Parameters:
    - s: Input string containing words separated by spaces.

    Returns:
    - str: The string with reversed word order.
    """
    # Your code here

Example:

assert reverse_words("Hello World") == "World Hello"
assert reverse_words("Coding Interview Practice") == "Practice Interview Coding"
assert reverse_words("Python is fun") == "fun is Python"

Note:

  • The input string contains only printable ASCII characters.
  • Words are separated by a single space.
  • The input string may start or end with spaces.
  • If possible, the solution should have a time complexity of O(n), where n is the length of the input string.
  • If possible, do not use any built-in functions or libraries that directly solve this problem. Implement the solution manually.

Instructions:

  • Write the reverse_words function to solve the problem.
  • Implement your solution in Python.
  • Provide clear comments in your code.
  • Discuss the time and space complexity of your solution.

As always, we’ll share our solution to this problem tomorrow. Stay tuned 😊