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: n = 5, roads = [[0,1], [0,3], [1,2], [1,3], [2,3], [2,4]]
Output: 5
Input: n = 8, roads = [[0,1], [1,2], [2,3], [2,4], [5,6], [5,7]]
Output: 5
EDGE CASE
Input: n = 4, roads = []
Output: 0
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 Problems, common solution patterns include:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a hashtable of size n which stores sets for each city. Members of the sets are cities which are directly connected to the city the set corresponds to. Then, examine all unique pairs (city_1, city_2) and sum up the edges of both cities. If there is an edge between both cities, the sum needs to be reduced by one since the edge is counted twice. We store the highest sum of edges and return it.
1. Use a hashtable of size n which stores sets for each city. Members of the sets are cities which are directly connected to the city the set corresponds to.
2. Then, examine all unique pairs (city_1, city_2)
3. Sum up the edges of both cities.
4. If there is an edge between both cities, the sum needs to be reduces by 1 since the edge is counted 2x.
5. Then, store the highest sum of edges and return it.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
# use a set to store the neighbors
city_to_cities = [ set() for i in range( n ) ]
max_network_rank = 0
# check each pair of cities, add their ranks together
# For each (i, j) pair, if i is the neighbor of j or the vice versa,
# we minus 1 on the rank
for road in roads:
city_to_cities[ road[0] ].add( road[1] )
city_to_cities[ road[1] ].add( road[0] )
for city_1 in range( n ):
for city_2 in range( city_1 + 1, n ):
network_rank = len( city_to_cities[city_1] ) + len( city_to_cities[city_2] )
if ( city_1 in city_to_cities[city_2] ):
network_rank -= 1
max_network_rank = max(max_network_rank, network_rank)
return max_network_rank
class Solution {
private Map<Integer, Set<Integer>> graph;
public int maximalNetworkRank(int n, int[][] roads) {
graph = new HashMap<>();
for(int i = 0; i < n; i += 1) graph.put(i, new HashSet<>());
for(int[] road : roads) {
int a = road[0], b = road[1];
// edge insertions
graph.get(a).add(b);
graph.get(b).add(a);
}
int maxNetworkRank = 0;
// trying all possible pairs of cities
for(int i = 0; i < n; i += 1) {
for(int j = i + 1; j < n; j += 1) {
int city_a = i, city_b = j, networkRank = 0;
int degreeA = graph.get(city_a).size();
int degreeB = graph.get(city_b).size();
if(graph.get(city_a).contains(city_b)) {
networkRank = degreeA + degreeB - 1;
} else {
// not connected directly adjacently. Might be in the same component or in different components.
networkRank = degreeA + degreeB;
}
maxNetworkRank = Math.max(maxNetworkRank, networkRank);
}
}
return maxNetworkRank;
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume V
represents the number of vertices.
Assume E
represents the number of edges