Common Reachable Node - Facebook Top Interview Questions
Problem Statement :
You are given a two-dimensional list of integers edges representing a directed graph and integers a and b. Each element in edges contains [u, v] meaning there's an edge from u to v. Return whether there's some node c such that we can go from c to a and c to b. Constraints 1 ≤ n ≤ 100,000 where n is the length of edges Example 1 Input edges = [ [0, 4], [4, 3], [1, 2], [0, 1], [0, 2], [1, 1] ] a = 2 b = 3 Output True Explanation We can reach 2 and 3 from 0 Example 2 Input edges = [ [0, 1], [1, 2], [1, 3], [1, 1], [4, 5] ] a = 2 b = 4 Output False Explanation 2 and 4 are on different connected components. Example 3 Input edges = [ [0, 0] ] a = 0 b = 0 Output True
Solution :
Solution in C++ :
vector<int> adj[100001];
vector<bool> vis(100001, false);
unordered_map<int, int> mp;
bool flag;
void dfs(int curr) {
vis[curr] = true;
mp[curr]++;
if (mp[curr] > 1) {
flag = true;
return;
}
for (int node : adj[curr])
if (!vis[node]) dfs(node);
}
bool solve(vector<vector<int>>& edges, int a, int b) {
int n = edges.size() + 1;
if (a == b) return true;
for (int i = 0; i < n; i++) adj[i].clear();
for (int i = 0; i < n - 1; i++) {
adj[edges[i][1]].push_back(edges[i][0]);
}
mp.clear();
flag = false;
for (int i = 0; i < n; i++) vis[i] = false;
dfs(a);
for (int i = 0; i < n; i++) vis[i] = false;
dfs(b);
return flag;
}
Solution in Java :
import java.util.*;
class Solution {
static ArrayList<Integer>[] transpose;
static int N;
public boolean solve(int[][] edges, int a, int b) {
N = 0;
for (int[] e : edges) N = Math.max(N, Math.max(e[0], e[1]));
N++;
// build the transpose of the graph
transpose = new ArrayList[N];
for (int i = 0; i < N; i++) transpose[i] = new ArrayList<Integer>();
for (int[] e : edges) transpose[e[1]].add(e[0]);
// find ancestors of A and B
boolean[] visA = bfs(a);
boolean[] visB = bfs(b);
// cehck if there is any overlap
for (int i = 0; i < N; i++) {
if (visA[i] && visB[i]) {
// node i is a common ancestor!
return true;
}
}
return false;
}
public boolean[] bfs(int root) {
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
boolean[] vis = new boolean[N];
vis[root] = true;
q.add(root);
while (!q.isEmpty()) {
int n = q.pollFirst();
for (int p : transpose[n]) {
if (!vis[p]) {
vis[p] = true;
q.add(p);
}
}
}
return vis;
}
}
Solution in Python :
class Solution:
def solve(self, edges, a, b):
graph = defaultdict(set)
for u, v in edges:
graph[v].add(u)
def explore(root):
queue = [root]
seen = {root}
for node in queue:
for nei in graph[node] - seen:
seen.add(nei)
queue.append(nei)
return seen
return bool(explore(a) & explore(b))
View More Similar Problems
Print in Reverse
Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing
View Solution →Reverse a linked list
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio
View Solution →Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →Merge two sorted linked lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C
View Solution →Get Node Value
This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t
View Solution →Delete duplicate-value nodes from a sorted linked list
This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -
View Solution →