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?
How do we encode the prerequisites into a graph?
When is it impossible for you to finish all tasks?
HAPPY CASE
Input: 2, [[1, 0]]
Output: true
Input: 2, [[1, 0], [0, 1]]
Output: false
EDGE CASE
Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: true
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.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Generate all possible tasks distribution among workers and compare with previous best result
1) in each visit, we start from a node and keep visiting its neighbors,
2) if at a time we return to a visited node, there is a cycle. Otherwise, start again from another unvisited node and repeat this process.
⚠️ Common Mistakes
indegree
0, rather than nodes with outdegree
0.Implement the code to solve the algorithm.
// returns adjacency list representation from a list of pairs
static ArrayList<ArrayList<Integer>> make_graph(int numTasks, Vector<pair> prerequisites) {
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>(numTasks);
for(int i=0; i<numTasks; i++){
graph.add(new ArrayList<Integer>());
}
for (pair pre : prerequisites)
graph.get(pre.second).add(pre.first);
return graph;
}
// a DFS based function to check if there is a cycle in the directed graph
static boolean dfs_cycle(ArrayList<ArrayList<Integer>> graph, int node, boolean onpath[], boolean visited[]) {
if (visited[node])
return false;
onpath[node] = visited[node] = true;
for (int neigh : graph.get(node))
if (onpath[neigh] || dfs_cycle(graph, neigh, onpath, visited))
return true;
return onpath[node] = false;
}
// main function to check whether possible to finish all tasks or not
static boolean canFinish(int numTasks, Vector<pair> prerequisites) {
ArrayList<ArrayList<Integer>> graph = make_graph(numTasks, prerequisites);
boolean onpath[] = new boolean[numTasks];
boolean visited[] = new boolean[numTasks];
for (int i = 0; i < numTasks; i++)
if (!visited[i] && dfs_cycle(graph, i, onpath, visited))
return false;
return true;
}
# returns adjacency list representation from a list of pairs
def make_graph(numTasks, prerequisites):
graph = []
for i in range(numTasks):
graph.append([])
for pre in prerequisites:
graph[pre.second].append(pre.first)
return graph
# a DFS based function to check if there is a cycle in the directed graph
def dfs_cycle(graph, node, onpath, visited):
if visited[node]:
return false
onpath[node] = visited[node] = True
for neigh in graph[node]:
if (onpath[neigh] or dfs_cycle(graph, neigh, onpath, visited)):
return true
return False
def canFinish(numTasks, prerequisites):
graph = make_graph(numTasks, prerequisites)
onpath = [False]*numTasks
visited = [False]*numTasks
for i in range(numTasks):
if (not visited[i] and dfs_cycle(graph, i, onpath, visited)):
return False
return True
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.
Time Complexity: O(E+V), where E is the number of dependencies and V is the number of tasks
Space Complexity: O(V), where V is the number of tasks