TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)
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?
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.
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:
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
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
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.
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.