Google Interview Question


Country: United States
Interview Type: Written Test




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

I have already mentioned a space based solution which some1 has downgraded without giving reason
the other solution without keeping space is:

for(each node in array)
if(node->left!=NULL &&  node->right!=NULL)
count++
else if(node->left==NULL && node->right==NULL)
count--;
else
do nothing;

- kkr.ashish February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code is assuming we can destroy the linked list :D there will be updation for deletion of every node in array

- kkr.ashish February 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Go through the linked list and build a hash table mapping each node to the node that follows it. Then go through the array and for each position, look at the next position and see whether the node there matches what the hash table says the next node should be. Increment a counter each time there's not a match (counter starts with 1 since we always say there's at least 1 cluster).

- eugene.yarovoi February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void findClusters(Node[] nodes) {
		int numberOfClusters = 1;
		LinkedHashSet<Node> runningCluster = new LinkedHashSet<Node>();
		for (int i = 0; i < nodes.length - 1; i++) {
			if (nodeMap.get(nodes[i].getVal()).getNext() == nodes[i + 1]
					|| nodeMap.get(nodes[i].getVal()).getPrev() == nodes[i + 1]) {
				runningCluster.add(nodes[i]);
				runningCluster.add(nodes[i+1]);
			} else {
				printList(runningCluster);
				runningCluster.clear();
				numberOfClusters++;
				runningCluster.add(nodes[i+1]);
			}
		}
		printList(runningCluster);
		System.out.println("Total number of clusters are " + numberOfClusters);
	}

private static Node createDoublyLinkedList(int[] array) {
		Node head = null, temp = null;
		for (int num : array) {
			if (head == null) {
				head = new Node(num, null, null);
				temp = head;
				nodeMap.put(num, head);
			} else {
				Node newNode = new Node(num, null, temp);
				temp.setNext(newNode);
				temp = newNode;
				nodeMap.put(num, newNode);
			}
		}
		return head;
	}
// pretty printing
	private static void printList(LinkedHashSet<Node> runningCluster) {
		System.out.print("(");
		for(Node node : runningCluster) {
			System.out.print( node.getVal() + " ");
		}
		System.out.print(")");
		System.out.println();
	}

Test cases I tried are

1) Linked list 1=>2=>3=>4=>5=>6=>7
Nodes[] = 1, 2, 4, 5 ,6 ;
output
(1 2 )
(4 5 6 )
Total number of clusters are 2

2) Linked list 1=>2=>3=>4=>5=>6=>7
Nodes[] = 1, 4, 5 ;

output

(1 )
(4 5 )
Total number of clusters are 2

- Anonymous February 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution would work even if the nodes are in reverse order for example

Node[] = {1,5,4} It will still return 2 clusters

- Anonymous February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Consider this as a tree with only root having two children. Given a node, if I do inorder traversal, I would get the cluster in order. How would I know when to stop forming cluster? I keep going left right and stop looking in a direction if the node is not listed in array or is pointing to null.

package com.google.practice;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

class Server implements Comparable<Server>{
	public Server left;
	public Server right;
	public String name;
	
	public Server(String name){
		this.name = name;
	}

	@Override
	public int compareTo(Server s) {
		// TODO Auto-generated method stub
		if(this.name.equals(s.name))
			return 0;
		else
			return ((int) Math.random())%2==0?-1:1;
	}
	
	public String toString(){
		return this.name;
	}
}

public class Cluster {
	public static void main(String[] arg){
		Server n1 = new Server("n1");
		Server n2 = new Server("n2");
		Server n3 = new Server("n3");
		Server n4 = new Server("n4");
		Server n5 = new Server("n5");
		Server n6 = new Server("n6");
		n1.left = null; n1.right = n2;
		n2.left = n1; n2.right = n3;
		n3.left = n2; n3.right = n4;
		n4.left = n3; n4.right = n5;
		n5.left = n4; n5.right = n6;
		n6.left = n5; n6.right = null;
		
		Server[] network = {n1,n4,n5};
		Set<Server> netSet = new TreeSet<Server>();
		for(int i=0;i<network.length;i++){
			netSet.add(network[i]);
		}
		List<List<Server>> clusters = new ArrayList<List<Server>>();
		findClusters(netSet,clusters);
		System.out.println(clusters.toString());
	}
	
	public static void findClusters(Set<Server> netSet,List<List<Server>> clusters){
		Iterator<Server> it = netSet.iterator();
		while(it.hasNext()){
			Server s = it.next();
			List<Server> cluster = new ArrayList<Server>();
			netSet.remove(s);
			findConnected(s,netSet,cluster);
			clusters.add(cluster);
			it = netSet.iterator();
		}
	}
	
	public static void findConnected(Server s, Set<Server> netSet, List<Server> cluster){
		if(s.left!=null && netSet.contains(s.left)){
				netSet.remove(s.left);
				findConnected(s.left,netSet,cluster);
		}
		cluster.add(s);
		if(s.right!=null && netSet.contains(s.right)){
			netSet.remove(s.right);
			findConnected(s.right,netSet,cluster);
		}
	}
}

- AlgoAlgae April 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can provide the array of nodes in any order. Adding to it, I also shuffle them up again (See compareTo())

- AlgoAlgae April 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i guess the array of nodes is unordered or else its just a simple question

on a simple thought:
can create a bool a[n] = {false}
and then set it for every occurance in the array then rescan the bool array and we will have the count

- kkr.ashish January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

By cluster do they mean greater than 1 successive node?

if yes, high level:
- clusterCount = 0
- Sort the Array (if not sorted - nLogN)
- foreach node in the NodeList, do a BinarySeach on the Array nLogN
- increment the clusterCount only if two or greater nodes are found

- Mo January 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

There are 2 possible questions:
1) If you cannot shuffle the array. Then we just use 2 pointers.
2) If we can shuffle the array. (This algorithm apparently solves first case as well). We put all values from array to hash map. Then go through the linked list and check if we have node in the hash map.

- Alex February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

n1 n4 n5 are pointers what are you gonna do with shuffling some pointers.. what makes you think pointers are ordered....

- kkr.ashish February 01, 2014 | Flag


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