Codepath

Mutual Friends

TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10 mins
  • 🛠️ Topics: Classes, Iteration, List Operations

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?
  • What is the goal of the function?
    • To find mutual friends between the current villager and a new contact.
  • What should the function return if there are no mutual friends?
    • It should return an empty list.
HAPPY CASE
Input: bob.friends = [stitches, raymond, fauna], marshal.friends = [raymond, ankha, fauna]
Output: ["Raymond", "Fauna"]
Explanation: Bob and Marshal have two mutual friends: Raymond and Fauna.

EDGE CASE
Input: bob.friends = [stitches, raymond, fauna], ankha.friends = [marshal]
Output: []
Explanation: Bob and Ankha have no mutual friends.

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 List Intersection problems, we want to consider the following approaches:

  • Use a loop to compare elements in both lists.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Iterate through the friends list of the current villager and check if they are also in the friends list of the new contact.

1) Initialize an empty list to store mutual friends.
2) Iterate through the friends list of the current villager.
3) For each friend, check if they are also in the friends list of the new contact.
4) If a friend is found in both lists, add their name to the mutuals list.
5) Return the mutuals list.

⚠️ Common Mistakes

  • Forgetting to check if the friends list is empty for either villager.
  • Incorrectly comparing objects instead of their attributes.

4: I-mplement

Implement the code to solve the algorithm.

class Villager:
    def __init__(self, name, species, catchphrase):
        self.name = name
        self.species = species
        self.catchphrase = catchphrase
        self.friends = []

    def get_mutuals(self, new_contact):
        mutuals = []
        for friend in self.friends:
            if friend in new_contact.friends:
                mutuals.append(friend.name)
        return mutuals

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 your code with an input to check for the expected output.
  • Catch possible edge cases and off-by-one errors.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work. Assume N represents the number of friends in the current villager's list and M represents the number of friends in the new contact's list.

  • Time Complexity: O(N * M) because we need to check each friend of the current villager against all friends of the new contact.
  • Space Complexity: O(L) where L is the number of mutual friends."
Fork me on GitHub