Get Full Names in Python (Time Complexity: O(n))


Given two arrays of strings named firstNames and lastNames, return an array containing the full names (separated by one space)

Example:

Input:   firstNames = ["John","Andy","Mary"]
         lastNames = ["Doe","Smith","Johnson"]

Output:  ["John Doe", "Andy Smith", "Mary Johnson"]

Understanding the Problem

The core challenge of this problem is to combine two arrays of strings into a single array of full names. Each full name is formed by concatenating the corresponding elements from the firstNames and lastNames arrays, separated by a space.

This problem is significant in many real-world applications, such as generating user profiles, creating contact lists, or formatting names for display purposes. A common pitfall is to assume that the arrays are always of the same length without validating this assumption.

Approach

To solve this problem, we can use a simple loop to iterate through the arrays and concatenate the corresponding elements. Here’s a step-by-step approach:

  1. Initialize an empty list to store the full names.
  2. Iterate through the indices of the firstNames array.
  3. For each index, concatenate the corresponding elements from firstNames and lastNames with a space in between.
  4. Append the concatenated string to the list of full names.
  5. Return the list of full names.

This approach ensures that we process each element exactly once, making it efficient with a time complexity of O(n), where n is the number of elements in the arrays.

Algorithm

Here’s a detailed breakdown of the algorithm:

  1. Initialize an empty list fullNames.
  2. Loop through the range of the length of firstNames (assuming both arrays are of the same length).
  3. For each index i, concatenate firstNames[i] and lastNames[i] with a space in between.
  4. Append the concatenated string to fullNames.
  5. Return fullNames.

Code Implementation

def get_full_names(firstNames, lastNames):
    # Initialize an empty list to store full names
    full_names = []
    
    # Iterate through the indices of the firstNames array
    for i in range(len(firstNames)):
        # Concatenate the corresponding elements from firstNames and lastNames
        full_name = firstNames[i] + " " + lastNames[i]
        # Append the full name to the list
        full_names.append(full_name)
    
    # Return the list of full names
    return full_names

# Example usage
firstNames = ["John", "Andy", "Mary"]
lastNames = ["Doe", "Smith", "Johnson"]
print(get_full_names(firstNames, lastNames))  # Output: ["John Doe", "Andy Smith", "Mary Johnson"]

Complexity Analysis

The time complexity of this approach is O(n), where n is the number of elements in the firstNames array. This is because we iterate through each element exactly once. The space complexity is also O(n) due to the storage of the full names in a new list.

Edge Cases

Potential edge cases include:

Example of handling empty arrays:

print(get_full_names([], []))  # Output: []

Testing

To test the solution comprehensively, consider the following test cases:

Example test cases:

assert get_full_names(["John", "Andy", "Mary"], ["Doe", "Smith", "Johnson"]) == ["John Doe", "Andy Smith", "Mary Johnson"]
assert get_full_names([], []) == []
# Add more test cases as needed

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to solve the problem of concatenating two arrays of strings to form full names. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

Additional Resources

For further reading and practice, consider the following resources: