Google Interview Question for Software Engineer / Developers






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

Method 1: hashtable
space complexity: O(n). time complexity: O(n)

Method 2: sort then scan
(1) sort two arrays, O(nlogn + mlogm)
(2) use to pointers to scan the two arrays. when finding two same values, stop and return. O(n+m)
time complexity: O(nlogn+mlogm)

- eboyGTK April 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hash contents of array1.
on second array if collision, increment value
linear search through hash and all those keys with value >1 are in both arrays

- t January 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool hasSame(int* a, int* b, int la, int lb){
    sort(a, la);
    sort(b, lb);
    int i=0, j=0;
    while(i<la && j<lb){
        if(a[i] == b[j]) return true;
        if(a[i] > b[j]) j++;
        else i++;
    }
    return false;
}

void sort(int* a, int la){
    int k = rand(la); // from 0 to la-1
    int spliter = a[k];
    int i=0, j=la-1;
    while(i<j){
        if(a[i]<=a[k]) i++;
        if(a[j]>a[k]) i--;
        if(a[i]>a[k] && a[j]<=a[k]){
            int swap = a[i];
            a[i] = a[j];
            a[j] = swap;
        }
    }
    sort(a,i);
    sort(a+i, la-i);
}

- Anonymous February 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ t, there is a small change in ur logic. Instead of incrementing for the 2nd array, decrement the value of hash based on 2nd array. if all the values are 0, then the arrays are similar

- Vijgan February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

take xor of both arrays and compare ?

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

Make an assoicative array.Init to zero.
Go thru first array --> associative_array[ arr1[index] ]++ ;
Go thru second array --> associative_array[ arr2[index] ]-- ;

if all elements in associative_array are not zero, return false...

- game. March 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if there are repeated elements in the first array?
arr1 = {1,2,2,3}, arr2={4}
The above approaches will return {2,4} -> incorrect !

- neo March 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
Using all possible early exit points with a hash table solution suggested by @t and improved by @Vijgan. In JavaScript: {{{​Array.prototype.equals = function(other)​ { if(this.length != other.length) { return false; } var value, hash = {}; for(var i = 0; i < this.length; i++) { value = this[i]; if(typeof hash[value] == 'undefined') { hash[value] = 0; } hash[value]++; } for(var i = 0; i < other.length; i++) { value = other[i]; if(typeof hash[value] == 'undefined') { return false; } hash[value]--; } for(var key in hash) { if(hash[key] != 0) { return false; } } return true; };}}] {{{[1,2,3].equals([3,1,2]) // true [].equals([]) // true [1,3,55].equals([3,55]) // false}}} - Anurag June 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can exit earlier and no need to go through hash at the end:

Just add "if (hash[value] != 0) return false"
Then you can delete
" for(var key in hash) {
if(hash[key] != 0) {
return false;
}
} "
We can permit this because the 2 arrays got the same length and because you did
"if(typeof hash[value] == 'undefined') {"

- hubert June 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we take sum and product for both arrays and compare?

- Nams September 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wat if the input is A{2,2,2,2} and B{4,4} Here the sum n product wud gv us same bt they are not...

- Ashwin March 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

xor elements of A first, if result is 0, this will be useless. if not 0 keep xoring elements of B if you get 0 they are equal.

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

Following two steps, both must be true
1)find sum of individual arrays, both sums must be equal
2)Now xor both the arrays, xor must be 0

If both above conditions are satisfied,arrays contain same elements
Complexity: O(n)

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

Take XOR of both array if contents are same XOR will result in zero

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

This is wrong.

a[] = {0, 0}
b[] = {1, 1}

XOR of a and b would be zero but they are not the same.

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

Following two steps, both must be true
1)find sum of individual arrays, both sums must be equal
2)Now xor both the arrays, xor must be 0

If both above conditions are satisfied,arrays contain same elements
Complexity: O(n)

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

Following THREE steps, all must be true
1)find sum of individual arrays, both sums must be equal
2)find product of both arrays,product must also be equal
3)Now xor both the arrays, xor must be 0

If ALL above conditions are satisfied,arrays contain same elements
Complexity: O(n)

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

Sorry ,but this is the final algo
Following Four steps, all must be true
1)find sum of individual arrays, both sums must be equal
2)find product of both arrays,product must also be equal
3)Now xor both the arrays, xor must be 0
4)And both arrays must contain the same number of elements
If ALL above conditions are satisfied,arrays contain same elements
Complexity: O(n)

- Anonymous June 25, 2012 | 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