Codepath

The Feeling is Mutual

Unit 10 Session 1 Standard (Click for link to problem statements)

Unit 10 Session 1 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 15-20 mins
  • 🛠️ Topics: Graphs, Adjacency Matrix, Symmetry

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 does each value in the adjacency matrix represent?
    • A: Each value celebrities[i][j] == 1 indicates that celebrity i likes celebrity j, while celebrities[i][j] == 0 indicates they dislike or don't know each other.
  • Q: How can we determine if a relationship is mutual?
    • A: A relationship between celebrity i and celebrity j is mutual if celebrities[i][j] == celebrities[j][i] == 1.
  • Q: What should we return if one or more relationships are not mutual?
    • A: Return False if any relationship is not mutual, otherwise return True.
HAPPY CASE
Input: 
celebrities = [
                [0, 1, 1, 0],
                [1, 0, 1, 0],
                [1, 1, 0, 1],
                [0, 0, 1, 0]]
Output: True
Explanation: All relationships between celebrities are mutual.

EDGE CASE
Input: 
celebrities = [
                [0, 1, 1, 0],
                [1, 0, 0, 0],
                [1, 1, 0, 1],
                [0, 0, 0, 0]]
Output: False
Explanation: Celebrity 0 likes celebrity 2, but celebrity 2 does not like celebrity 0 back.

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

  • Matrix Symmetry: Since mutual relationships are represented by symmetric values in the adjacency matrix, we need to check if the matrix is symmetric with respect to the diagonal.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: To check if all relationships are mutual, we need to ensure that for each pair of celebrities i and j, if celebrities[i][j] == 1 (i.e., i likes j), then celebrities[j][i] == 1 (i.e., j likes i back).

1) Determine the number of celebrities `n` from the length of the adjacency matrix.
2) Iterate through each pair of celebrities `i` and `j`:
   a) If `celebrities[i][j] == 1` and `celebrities[j][i] == 0`, return `False` because the relationship is not mutual.
3) If no non-mutual relationships are found, return `True`.

⚠️ Common Mistakes

  • Forgetting to check for the symmetry of the matrix.
  • Misinterpreting relationships where i == j (a celebrity does not need to like themselves).

4: I-mplement

Implement the code to solve the algorithm.

def is_mutual(celebrities):
    n = len(celebrities)  # Number of celebrities
    
    # Check if the matrix is symmetric
    for i in range(n):
        for j in range(n):
            # If celebrity i likes celebrity j, check that j also likes i
            if celebrities[i][j] == 1 and celebrities[j][i] == 0:
                return False
    
    return True

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Input:
    celebrities1 = [
                    [0, 1, 1, 0],
                    [1, 0, 1, 0],
                    [1, 1, 0, 1],
                    [0, 0, 1, 0]]
    print(is_mutual(celebrities1))
  • Output: True

  • Input:

    celebrities2 = [
                    [0, 1, 1, 0],
                    [1, 0, 0, 0],
                    [1, 1, 0, 1],
                    [0, 0, 0, 0]]
    print(is_mutual(celebrities2))
  • Output: False

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 number of celebrities. We need to check every pair of celebrities for mutual relationships.
  • Space Complexity: O(1) since we only use a few variables for comparison.
Fork me on GitHub