Google Interview Question for Software Developers


Country: Switzerland




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

Store Graph as follows

class GraphNode{
		public int value;
		Hashset<GraphNode> neighbours;
	}

	//Add all graph nodes to a list or something

Now go to each graph node and among its neighbours , which are say N
try to find two neighbours which are neighbour of each other , this forms a triangle.. this will be N square for each node having N neighbours , so worst case graph with N nodes having N-1 neighbours each , N^3 is the solution

- smarthbehl July 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Express your graph as a 2D array (hint: you only need top part).

size_t countTriangles(const bool** g, size_t n)
{
	auto res = 0;
	for (auto row = 0; row < n - 1; ++row) {
		for (auto col = 0; col < n - 1; ++col) {
			if (g[row][col]) {
				for (auto match = col + 1; match < n; ++match) {
					if (g[row][match] && g[match][col]) { ++res; }
				}
			}
		}
	}
	return res;
}

The complexity is n^3 as there is a maximum if n^3 triplets.

- PhDude July 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Every triangle is double counted in this solution.

- Alex M. October 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package cup;

import java.util.*;
import java.util.Map.Entry;

public class CountTraingles {

	public static void main(String[] args) {
		System.out.println(getTraingleCount(getGraph()));

	}
	
	public static int getTraingleCount(Map<Node<Character>, Set<Node<Character>>> map){
		
		int count=0;
		Set<Node<Character>> visitedNodeSet = new HashSet<>();
		for(Entry<Node<Character>, Set<Node<Character>>> entry : map.entrySet()){
			Set<Node<Character>> neighbourSet = entry.getValue();
			removeVisitedValFromList(neighbourSet, visitedNodeSet);
			List<Node<Character>> neighbourList = new ArrayList<>(neighbourSet);
			for(int i=0; i<neighbourList.size(); i++){
				for(int j=i+1; j<neighbourList.size(); j++){
					if(checkIfPathExistsBetweenNeighbourNodes(neighbourList.get(i), neighbourList.get(j), map)){
						count++;
					}
				}
			}
			visitedNodeSet.add(entry.getKey());
		}
		return count;
	}
	
	private static boolean checkIfPathExistsBetweenNeighbourNodes(Node<Character> node1, Node<Character> node2, Map<Node<Character>, Set<Node<Character>>> map){
		return (map.containsKey(node1) && map.get(node1).contains(node2));
	}
	
	private static void removeVisitedValFromList(Set<Node<Character>> neighbourList, Set<Node<Character>> visitedNodeSet){
		for(Node<Character> node: visitedNodeSet){
			neighbourList.remove(node);
		}
	}
	
	public static Map<Node<Character>, Set<Node<Character>>> getGraph(){
		Map<Node<Character>, Set<Node<Character>>> map = new HashMap<>();
		Set<Node<Character>> list = new HashSet<>();
		list.add(new Node<>('B'));
		list.add(new Node<>('C'));
		list.add(new Node<>('D'));
		map.put(new Node<>('A'), list);
		list = new HashSet<>();
		list.add(new Node<>('C'));
		list.add(new Node<>('A'));
		list.add(new Node<>('E'));
		list.add(new Node<>('D'));
		map.put(new Node<>('B'), list);
		list = new HashSet<>();
		list.add(new Node<>('A'));
		list.add(new Node<>('B'));
		map.put(new Node<>('C'), list);
		list = new HashSet<>();
		list.add(new Node<>('B'));
		list.add(new Node<>('D'));
		map.put(new Node<>('E'), list);
		list = new HashSet<>();
		list.add(new Node<>('A'));
		list.add(new Node<>('E'));
		list.add(new Node<>('B'));
		map.put(new Node<>('D'), list);
		return Collections.unmodifiableMap(map);
	}

	static class Node<T>{
		T node;
		public Node(T node){
			this.node = node;
		}
		
		@Override
		public int hashCode(){
			return node.hashCode();
		}
		
		@Override
		public boolean equals(Object o){
			if(o == null || !(o instanceof Node)){
				return false;
			}
			return ((Node<?>)o).node == node; 
		}
	}
}

- infinity July 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package cup;

import java.util.*;
import java.util.Map.Entry;

public class CountTraingles {

	public static void main(String[] args) {
		System.out.println(getTraingleCount(getGraph()));

	}
	
	public static int getTraingleCount(Map<Node<Character>, Set<Node<Character>>> map){
		
		int count=0;
		Set<Node<Character>> visitedNodeSet = new HashSet<>();
		for(Entry<Node<Character>, Set<Node<Character>>> entry : map.entrySet()){
			Set<Node<Character>> neighbourSet = entry.getValue();
			removeVisitedValFromList(neighbourSet, visitedNodeSet);
			List<Node<Character>> neighbourList = new ArrayList<>(neighbourSet);
			for(int i=0; i<neighbourList.size(); i++){
				for(int j=i+1; j<neighbourList.size(); j++){
					if(checkIfPathExistsBetweenNeighbourNodes(neighbourList.get(i), neighbourList.get(j), map)){
						count++;
					}
				}
			}
			visitedNodeSet.add(entry.getKey());
		}
		return count;
	}
	
	private static boolean checkIfPathExistsBetweenNeighbourNodes(Node<Character> node1, Node<Character> node2, Map<Node<Character>, Set<Node<Character>>> map){
		return (map.containsKey(node1) && map.get(node1).contains(node2));
	}
	
	private static void removeVisitedValFromList(Set<Node<Character>> neighbourList, Set<Node<Character>> visitedNodeSet){
		for(Node<Character> node: visitedNodeSet){
			neighbourList.remove(node);
		}
	}
	
	public static Map<Node<Character>, Set<Node<Character>>> getGraph(){
		Map<Node<Character>, Set<Node<Character>>> map = new HashMap<>();
		Set<Node<Character>> list = new HashSet<>();
		list.add(new Node<>('B'));
		list.add(new Node<>('C'));
		list.add(new Node<>('D'));
		map.put(new Node<>('A'), list);
		list = new HashSet<>();
		list.add(new Node<>('C'));
		list.add(new Node<>('A'));
		list.add(new Node<>('E'));
		list.add(new Node<>('D'));
		map.put(new Node<>('B'), list);
		list = new HashSet<>();
		list.add(new Node<>('A'));
		list.add(new Node<>('B'));
		map.put(new Node<>('C'), list);
		list = new HashSet<>();
		list.add(new Node<>('B'));
		list.add(new Node<>('D'));
		map.put(new Node<>('E'), list);
		list = new HashSet<>();
		list.add(new Node<>('A'));
		list.add(new Node<>('E'));
		list.add(new Node<>('B'));
		map.put(new Node<>('D'), list);
		return Collections.unmodifiableMap(map);
	}

	static class Node<T>{
		T node;
		public Node(T node){
			this.node = node;
		}
		
		@Override
		public int hashCode(){
			return node.hashCode();
		}
		
		@Override
		public boolean equals(Object o){
			if(o == null || !(o instanceof Node)){
				return false;
			}
			return ((Node<?>)o).node == node; 
		}
	}
}

- infinity July 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry for the duplicate entry !!

- infinity August 22, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

list variable is actually a SET here.. sorry for confusion

- infinity August 22, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int countTriangles(int[][] graph) {
        int count = 0;
        for (int v = 0; v < graph.length; v++) {
            for(int e1 = v + 1; e1 < graph.length; e1++) {
                if(graph[v][e1] == 0) {
                    continue;
                }
                for(int e2 = e1 + 1; e2 < graph.length; e2++) {
                    if(graph[v][e2] == 0) {
                        continue;
                    }
                    count += graph[e1][e2] == 1 ? 1 : 0;
                }
            }
        }
        return count;
    }

- Rocky August 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int countTriangles(int[][] graph) {
        int count = 0;
        for (int v = 0; v < graph.length; v++) {
            for(int e1 = v + 1; e1 < graph.length; e1++) {
                if(graph[v][e1] == 0) {
                    continue;
                }
                for(int e2 = e1 + 1; e2 < graph.length; e2++) {
                    if(graph[v][e2] == 0) {
                        continue;
                    }
                    count += graph[e1][e2] == 1 ? 1 : 0;
                }
            }
        }
        return count;
    }

- Rocky August 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use dfs to solve this problem. Code as follows, assume that V is the number of vertex.

static const int V = 100010;

vcetor<int> G[V];
bool visited[V];

void dfs(int u, int par, int grand, vector<vector<int> > &ans) {
	visited[u] = true;
	for (int v : G[u]) {
		if (v == grand) {
			ans.push_back({u, par, grand});
		}
		else if (!visited[v])
			dfs(v, u, par, ans);
	}
}

vector<vector<int> > Triangles() {
	vector<vector<int> > ans;
	for (int i = 0; i < V; ++i) {
		if (!visited[i])
			dfs(i);
	}

	return move(ans);
}

- malinbupt September 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You should Build the Adjacency matrix of the graph let's call it A, then compute the matrix B=A^3. The entry (i,j) in B is the number of paths of length 3 from i to j, thus the answer is trace(B)/6.

Complexity is the complexity of matrix multiplication which is about n^2.4

- Elad September 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check out this least voted answer.

- Anonymous October 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check out this least voted answer.

- Anonymous October 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1. run a variant of DFS on each node and count the edges while traversing
If you happen to visit the visit the start node with edge count = 3 , then the graph has cycles.

- vivekh August 31, 2015 | 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