Unit 10 Session 1 Standard (Click for link to problem statements)
Unit 10 Session 1 Advanced (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?
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.i
and celebrity j
is mutual if celebrities[i][j] == celebrities[j][i] == 1
.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.
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:
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
i == j
(a celebrity does not need to like themselves).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
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
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
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(n^2)
where n
is the number of celebrities. We need to check every pair of celebrities for mutual relationships.O(1)
since we only use a few variables for comparison.