Google Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

count the bits or 1s in the stream of each 32bits positions.
you will end up with 32 counts of 1s (each representing the number of 1s seen at the bit position).
Compare it with m/2. If it is bigger, that bit is 1. else that bit is 0.

- hsong November 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1: A remarkably simple yet clever algorithm.

- eugene.yarovoi November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if the numbers are all odd and unique. then bit0 count will be 32. The above algorithm would printout a wrong answer right?

- infinitystreet November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Can you be more specific. I am not able to get exactly what you are trying to explain

- Praveen Kumar K J V S November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hsong's algorithm says: count the number of times each bit is 1 across all the numbers (store in arr count[]). After reading m numbers, count[0] will give the number of times bit 0 is 1 among all m numbers. If greater than m/2, set the output element bit 0 to 1.

Output should be the number with freq > m/2. If the input is a stream of unique odd numbers then count[0] will be 32. What do we print output in this case?

- infinitystreet November 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this won't work when we have all numbers odd & distinct

- cipher November 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This approach doesn't work! The frequency of the element of interest is at least M/2, which can very well be exactly M/2. Thus, if the count of some bit is M/2, that bit can be either 0 or 1. In the worst case, all 32 bits have counts equal to M/2 which makes all the 2^32 combinations feasible.

- JuggNutt November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Remarkably clever algorithm. I think it works in all cases, even when all numbers are odd as suggested in above posts.

To infinitystreet, cipher and JuggNutt: The frequency of the element of interest is larger than m/2 (not "at least m/2"). Thus it will not happen that all numbers are unique.

- RunningOn December 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let's say that we have three numbers, 1, 3 and 4. The count is going to be 1 1 2 for the last 3 bits, 0 for the rest. Then, since m/2 is 1.5, this approach will give 1 as a result which is an incorrect result.

- Test January 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Algo is superb !!! Too bad some aren't getting it !

- Ravikant January 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

consider a sequence 3, 14, 10, 2
0011 = 3
1110 = 14
1010 = 10
0010 = 2
--------
summing up bits set to 1 gives us:
--------
2141

according to your algo, the repeated number is 2.
which is wrong.

am I missing sth?

- Anonymous March 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your missing that the problem statement guarantees that there is one number with > m/2 entries!

- tommy April 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is simple? Yes. Clever? Well... that is subjective.

But the problem with this is that this is O(M w) where w is the word size of the underlying machine, and assumes we actually know what it is and actually uses O(w log M) space.

- Anonymous September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It works

you have to set all the bits having count > m/2
if bit 0 and bit 3 repeat more than m/2
then ans is 1001 = 9

- mos September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think Anonymous above was saying it doesn't work...just that it's less efficient than the most optimal solution, which is true.

It's still a relatively efficient solution, however, given that people are probably assuming here that w is a constant. I think the reason most people (including myself) upvoted this is because this was a fresh take on a classic problem.

- eugene.yarovoi September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can someone please explain with an example. Not able to get it.

- sandy October 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

#include<stdio.h>

int main() {
int c=0,id=-1;
int n;
while(printf("Enter a number:") && scanf("%d",&n)){
if(id != -1 && id != n)
c--;
else if(id == n)
c++;
if(c == 0){
id=n;
c++;
}
}
printf("The number is : %d",id);
return 0;
}

- ALgeek November 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

is this code really working? I dont think so ...

- Odi December 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

class MajorityVoting
{
public static void main (String args[])
{
int arr[] = {2, 2, 3, 3, 3, 2, 2, 3, 2, 3};
System.out.println (findMajor (arr));
}

public static int findMajor(int[] arr)
{
int curr = arr[0];
int count = 1;
for (int i=0; i< arr.length; i++)
{
if (curr == arr[i])
count++;
else
count--;
if (count == 0 && (i+1) < arr.length)
{
curr = arr[i+1];
count = 0;
}
System.out.println (arr[i] + " " +curr+ " " + count);
}
if (count <= 0)
return -1;
return curr;
}
}

- someone December 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This will work if freq[j]>m/2 always hold.

int FindMajority(int *arr, int n)
{
	int count = 0;
	int major = 0;

	while (n--)
	{
		if (count == 0 || major == *arr)
		{
			count++;
			major = *arr;
		}
		else
		{
			--count;
		}

		++arr;
	}

	return major;
}

- Anonymous November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can't understand the problem.
Can anybody explain it? a sample may be better.

- Anonymous November 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question in simpler terms:
In a collection of 'M' elements, some elements have repeated occurrences. Find the element which occurred at least M/2 times.

- Laxmi Narsimha Rao Oruganti November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private int findMaxFreq(ObjectInputStream in) {
int freq = 1;
int key = Integer.MINIMUM;
while ((temp = in.readInt())!=null){ // continue reading the stream till you exhaust it
if (temp == key) // its old number
   freq ++;
else {// its a new number
freq --;
if(freq <=0){
// time to change the key
key = temp;
}
}

After exhausting the stream, the number with freq > m/2 will be stored in the key.

- hv-monk November 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it similar to question with id 11879708? I am not able to post the link.

- sandeepgiri February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the array 'a'
for (i = 0; i < M; ++i)
{
    if (a[i] == a[ i + floor(M\2) - 1) ]
      ...print a[i] is repeated atleast M\2 times 
}

- Anonymous March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

isn't this the majority voting algorithm?

- citisushi July 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Its just the use of MOOR's voting algorithm.

- Nascent Coder November 17, 2011 | 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