Google Interview Question
Software EngineersCountry: India
int checkFriend(int s, int t) {
queue<int> q;
q.push(s);
int level = 0;
vector<bool> visited(maxN, false);
visited[s] = true;
while (q.size()) {
int n = q.size();
for(int i = 0; i < n; i++) {
int u = q.top();
q.pop();
if (u == t) return level;
for(int v: a[u]) if (!visited[v]) {
visited[v] = true;
q.push(v);
}
}
level++;
{
}
def findlist(input):
dictn={}
for pair in input:
dictn[pair[0]]=dictn.get(pair[0], [])+[(pair[2],pair[1])]
queue=[(0, 1)]
ans=[1]
while queue:
tmstmp, person = queue.pop(0)
if dictn.get(person) is not None:
for neighbour in dictn[person]:
if neighbour[0]>=tmstmp:
queue.append((neighbour[0], neighbour[1]))
ans.append(neighbour[1])
return ans
def getFriend(user):
#returns a list of users first degree friends
def checkdegree(user1, user2):
if user1==user2:
return 0
queue=[(user1, 0)]
degree=-1
while(queue):
usr, degree = queue.pop(0)
friendslist=getFriend(usr)
for friend in friendslist:
if friend==user2:
return degree+1
if degree<2:
queue.append((friend, degree+1))
return -1
def getFriends(user):
first_degree = getFriend(user)
ans=[]
for friend in firstdegree:
sec_degree = getFriend(friend)
ans.extend(sec_degree)
for friend2 in sec_degree:
ans.extend(getFriend(friend2))
return and
Example
friends_data_dict = {
a: [b,c]
b: [c,d]
d: [e,f]
p: [f,i]
j: [k,l]
}
a and b have 1 degree friendship
a and d are 2 degree friends
a and f are 3 degree friends
a and i are 4 degree friends
a and l are not friends
class Friend:
def __init__(self, name):
self.name = name
self.friends = []
def get_degree_of_friendship(friend_one: Friend, friend_two: Friend):
“””
say friend a is friend_one and f is friend_two
“””
friends_identified = _set()
friends_identified .add(friend_one.friends) #Here friends_identified = [b,c]
friends_queue_holder = []
friends_queue_holder.append(friend_one.friends)
friends_ordered_set = get_friend_sequence(
friend_two,
friends_identified,
friends_queue_holder
)
degree = len(friends_ordered_set)
print(“%s and %s are %s degree friends” % (friend_one, friend_two, degree))
def get_friend_sequence():
while friends_queue_holder:
friend = friends_queue_holder.pop(0)
if friend_two in friends_data_dict[friend]:
return friends_identified
friends_identified.add(friends_data_dict[friend])
friends_queue_holder = friends_data_dict[friend]
I would do it recursively. Store friends in a dictionary with degree as values.
def getFriendsRecursive(user, currentDegree, maxDegree)
friends = getFriends(user)
friendsWithDegree = {}
currentDegree += 1
foreach friend in friends:
friendsWithDegree[friend] = currentDegree
if(currentDegree <= maxDegree):
foreach friend in friends:
otherDegrees = getFriendsRecursive(friend, currentDegree, maxDegree)
foreach otherDegree in otherDegrees:
if not otherDegree in friendsWithDegree:
friendsWithDegree[otherDegree] = otherDegrees[otherDegree]
return friendsWithDegree
getFriendsRecursive(user, 0, 3)
we can have BFS way traversal and until we find the target node keep traversing:-
- vinod June 01, 2021void util(int src, int tgt, boolean vis[], int dist)
{
Queue<Integer> q = new LinkedList<Integer>();
q.add(src);
while(!q.isEmpty())
{
int x = q.pop();
vis[x] = true ;
ArrayList<Integer> adj = getFriends(x);
for(Integer k : adj)
{
if(!vis[k])
{
q.push(k);
dis[k]+=1;
}
}
}
int getDistance(int src , int tgt)
{
int dist[] = new int[v] // number of nodes
boolean vis[] = new boolean[] ;
util(src, tgt, vis, dist)
return dist[tgt];
}