Google Interview Question for Software Engineers


Country: Switzerland
Interview Type: Written Test




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

This method uses bitwise operators .It works for all inputs except when the said element is present in multiple of 3.

int func(vector<int>&a){
//Assuming 32-bit integers
    int result=0;
    for(int i=0;i<31;i++){
        int mask=1<<i;
        int count=0;
        for(int k=0;k<a.size();k++){
            if(a[k]&mask){
                count++;
            }//end if
        }//end for
        if(count%3){
            result|=mask;
        }//end if
    }//end for
    return result;
}//end func

- Klaus July 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

One way is to create a hash table with frequency of each integer and then search though it to find the non-compliant integer:

private static int find(int[] array) {
        HashMap<Integer, Integer> frequencies = new HashMap<>();
        for (int value : array) {
            Integer frequency = frequencies.get(value);
            if (frequency == null) {
                frequencies.put(value, 1);
            } else {
                if (frequency == 3) {
                    return value;
                }
                frequencies.put(value, 1 + frequency);
            }
        }
        for (int value : frequencies.keySet()) {
            if (frequencies.get(value) != 3) {
                return value;
            }
        }
        return 0;
    }

- tested.candidate July 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Who are you, tested.candidate?

- tested.candidate July 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I don't see the necessity for the last loop. If there is an outlander, you will exit during the first one.
If they were asking to find all of them, then the "return" in the first loop has to be replaced with adding the outlander to the result structure, and I still don't see the need for the last loop.

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

Actually the last loop is needed, since the value can be lower than 3 as well as more than 3, on top of that it might be not a single number, but bunch of them.

- Iliiazbek Akhmedov July 22, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use hashmap, for each integer, increment the hashmap value by one, once complete, try one scan to hashmap, and see which all integers does not setisfying this peroperties.

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

Sort and traverse. nlogn + n = nlogn

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

public class FindMissingNumber {

    public static void main(String[] args) {
        int[] numbers = {3, 3, 4, 0, 10, 3, 0, 10, 0, 10, 4};
        System.out.println(findMissing(numbers));
    }

    public static int findMissing(int[] numbers) {
        Set<Integer> unique = new HashSet<>();
        int uniqueSum = 0;
        int realSum = 0;

        for (int number : numbers) {
            realSum += number;

            if (!unique.contains(number)) {
                uniqueSum += number;
                unique.add(number);
            }
        }

        return 3 * uniqueSum - realSum;
    }
}

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

What if the array were
[0, 0] or [0, 0, 0, 1, 2, 2, 2] ? In those cases, I don't think your alg would work.

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

Thank you, for whatever reason I made a wrong assumption, that element can be present either 2 or 3 times.

- damluar July 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Assuming that the integers in the array are unsorted. If more then one integer is found that doesn't repeat 3 times, only the first occurrence will be returned.

public class DuplicateService
{
	public int findNonRepeat(int[] arr) 
	{
		if(arr==null)
		{
			throw new NullPointerException("input array cannot be null");
		}
	       if(arr.length<3)
		{
			return(arr[0]);
		}
		HashMap<Integer,Integer> counts=new HashMap<Integer,Integer>();
		for(int i=0;i<arr.length;i++)
		{
			if(!counts.contains(arr[i]))
			{
				counts.put(arr[i],1);
			}else
			{
				int count=counts.get(arr[i]).intValue();
				count++;
				counts.put(arr[i],count);
			}
	 	 }
		int result=0;
		for(Map.Entry<Integer,Integer> e:counts)
		{
			int x=e.getValue();
			if(x<3)
			{
				result=e.getKey().intValue();
			}
			return result;
		}
	}

}

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

If you got 2 of each, except for one. You could just xor the hole lot and then you would get the missing one.
Missing = Xor(all_others[i]) for all i from 0 to n-1;
xor (xor(n,x),x) == n
and
xor(n,x)== n
So you need some kind of a function such that
F(F(F(n,x),x ),x) == n
and
G(F(F(n,x),x)) == x
You should be able to do this with a little bit manipulation only storing 2 additional integers as well as a pointer into the array. These 2 integers are combined into try nary integer of the corresponding length. You add each integer to this number but you do not do any caries. When you add a 1 to on of these try nary (trits) it goes from 0 to 1 and then when you do it again it goes from 1 to 2 and then it goes back from 2 to 0. You do this for each bit in each number. If a number appears 3 times it will role that bace 3 digit around 3 times and not affect the result. But the digit that happens 2 times will role its bit to 2.

class Foo
{
public:
    Foo(): a(0), b(0) { }
    
    void put(int n)
    {
        int ap = ((~b) & (~a) & n) | ((~b) & a & (~n));
        b = ((~b) & a & n) | (b & (~a) & (~n));
        a = ap;
    }
    
    const int get()const
    {
        return b;
    }
    
private:
    int a,b;
    
};

/*
 * 
 */
int main(int argc, char** argv) {

    Foo f;
    
    f.put(12);
    f.put(12);
    f.put(12);
    
    f.put(1);
    f.put(1);
        
    f.put(2);
    f.put(2);
    f.put(2);
    
    cout << f.get();
    
    return 0;
}

- Dr A.D.M. July 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think it is hard to understand... but it's really cool.
we can transform the integer to 32-bit, and use a array to store the sum of each bit. at last, mod 3 and all the bit is not 0 is the special num 's bit.

- lxfuhuo July 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if a number occurs 6 times?

- Eidn July 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can create a list ehich contains unique elements from input list. Then iterate over the unique list and check the count of number, if not 3 then add to return list

from sets import Set 
def find_unmatched(list_num):
    unmatched_list = []
    unique_list = list(set(list_num))
    for number in unique_list:
        if list_num.count(number) != 3:
            unmatched_list.append(number)
    return unmatched_list

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

Here is my solution using bit manipulation:

public int findNumber(int[] nums) {
  int ones = 0, twos = 0, threes = 0;
  
  for (int i = 0; i < nums.length; i++) {
    int x = nums[i];
    
    twos |= ones & x;
    ones ^= x;
    threes = ones & twos;
    
    ones &= ~threes;
    twos &= ~threes;
  }
  
  return ones | twos;
}

ones holds the numbers that appear once, twos holds the number that appear twice, threes holds the numbers that appear three times.

- jean.timex July 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sample solution in C++

#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

int findDifferent(const vector<int>& v) {
  unordered_map<int, int> m;
  
  for (const auto n : v) {
    ++m[n];
  }

  for (const auto& p : m) { 
    if (p.second != 3) {
      return p.first;
    }
  }

  return -1;
}

int main() {
  vector<int> v {1,2,3,1,2,3,1,2,3,2};
  int different = findDifferent(v);
  cout << different << endl;

  return 0;
}

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

If count of the number which is not following rule is even:

numbers = [3, 3, 4, 0, 10, 3, 0, 10, 0, 10, 4]
unique_numbers = set()
mask = 0
for n in numbers:
    unique_numbers.add(n)
    mask ^= n
for n in unique_numbers:
    mask ^= n
print(mask)

- encoded October 05, 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