Google Interview Question for Software Engineers


Country: India




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

we can have BFS way traversal and until we find the target node keep traversing:-

void 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];



}

- vinod June 01, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Rudimentary. Just do DFS in any form with depth count ( max_depth ) and create visited set.
Do this for both user_1 and user_2.
Then simply do an intersection.
You are done.

- NoOne May 28, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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++;
	{

}

- kcin July 03, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- priyakoshta21 July 05, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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]

- manish.sharma0201cs September 05, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- Anonymous March 08, 2022 | Flag Reply


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