## Facebook Interview Question for SDE1s

• 0
of 0 votes

Country: United States

Comment hidden because of low score. Click to expand.
1
of 1 vote

To show friends or friends, build Graph with edges between relations.
Get the node with the id to be searched and traverse children. Mark nodes as 'traversed' once printed to prevent infinite loop.
Here is the code:

``````import java.util.*;

class Main {
public static void main(String[] args) {

List<List<Integer>> relations = new ArrayList<>();
relations.add(Arrays.asList(1, 2));
relations.add(Arrays.asList(2, 3));
relations.add(Arrays.asList(5, 6));
relations.add(Arrays.asList(1, 3));
relations.add(Arrays.asList(2, 4));
relations.add(Arrays.asList(4, 12));

printFriendCircle(relations.iterator(), 1);

}

public static void printFriendCircle(Iterator<List<Integer>> itr,int id){
Graph graph = new Graph();
while(itr.hasNext()){
List<Integer> edge = itr.next();
graph.addEdge(edge.get(0),edge.get(1));
}
Node root=graph.nodes.get(id);
printfriends(root);

}

public static void printfriends(Node n){
if(n==null)return;
for(Node child: n.children){
if(!child.traversed){
System.out.println(child.data+ " ");
child.traversed=true;
printfriends(child);
}
}

}
}

class Graph{
HashMap<Integer,Node> nodes = new HashMap<>();

public void addNode(int d){
Node n = new Node(d);
nodes.put(d,n);
}

public void addEdge(int a,int b){
if(nodes.get(a)==null)addNode(a);
if(nodes.get(b)==null)addNode(b);
(nodes.get(a).children).add(nodes.get(b));
(nodes.get(b).children).add(nodes.get(a));
}

}

class Node{
boolean traversed = false;
int data;
List<Node> children = new ArrayList<>();
public Node(int d){
data=d;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Is the question to find *immediate* friends only? Then its trivial.

``````private static List<Integer> getFriends(Iterator<List<Integer>> relations, int id) {
List<Integer> friends = new ArrayList<>();
while(relations.hasNext()) {
List<Integer> relation = relations.next();
if (relation.get(0) == id)
friends.add(relation.get(1));
else if (relation.get(1) == id)
friends.add(relation.get(0));
}
return friends;
}

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

However, if the problem is to include friends-of-friends as well, then we need to store all the friendships as they arrive

``````public class FriendshipStream {
public static void main(String[] args) {
List<List<Integer>> relations = new ArrayList<>();
relations.add(Arrays.asList(1, 2));
relations.add(Arrays.asList(2, 3));
relations.add(Arrays.asList(1, 3));
relations.add(Arrays.asList(2, 4));
relations.add(Arrays.asList(5, 6));

printFriendCircle(relations.iterator(), 1);
}

private static void printFriendCircle(Iterator<List<Integer>> relations, int id) {
Map<Integer, List<Integer>> friendships = buildMap(relations);
List<Integer> allFriends = findAllFriends(friendships, id);
for(int friend : allFriends)
System.out.println(friend);
}

private static Map<Integer, List<Integer>> buildMap(Iterator<List<Integer>> relations) {
Map<Integer, List<Integer>> friendships = new HashMap<>();
while(relations.hasNext()) {
List<Integer> relation = relations.next();
addFriendship(friendships, relation.get(0), relation.get(1));
addFriendship(friendships, relation.get(1), relation.get(0));
}
return friendships;
}

private static void addFriendship(Map<Integer, List<Integer>> friendships, int a, int b) {
List<Integer> friends = friendships.computeIfAbsent(a, k -> new ArrayList<>());
friends.add(b);
}

private static List<Integer> findAllFriends(Map<Integer, List<Integer>> friendships, int id) {
List<Integer> friends = new ArrayList<>();
List<Integer> next = Collections.singletonList(id);
while(!next.isEmpty()) {
List<Integer> curr = new ArrayList<>();
for(int friend : next) {
List<Integer> friendsOfFriend = friendships.getOrDefault(friend, Collections.emptyList());
for(int fOfF : friendsOfFriend)
if (!friends.contains(fOfF) && fOfF != id)
curr.add(fOfF);
}
friends.addAll(curr);
next = curr;
}
return friends;
}

}``````

Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

### Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

### Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More