Google Interview Question for Software Engineer / Developers






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

Hint : Merge method of Merge sort !!!

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

<pre lang="python" line="1" title="CodeMonkey82035" class="run-this">def pair(l):
for x in xrange(len(l)):
a = l[x]
for b in l[x:]:
if a > b:
print (a,b)

if __name__ == '__main__':
pair([8,3,6,10,5])</pre>

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

The running time of this algorithm is N(N+1)/2.
I think better solution exists ;)

- fiddler.g June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, this is brute force. Plus we just need the count. Dont have to print all the numbers.

- Software Engineer June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you check my next post, please? I hope that's correct ;)

- fiddler.g June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is pseudocode:

var sortedArray = new Array();
var count = 0;

foreach b in array
{
// insert b in already sorted array and get position (staring from 1)
// running time is log(sortedArray.length)
// can be implemented like binary-search

var pos = insert(sortedArray, b);

// all the elements in sortedArray located after pos are > b
// so those elements can appear in pair with b

count += sortedArray.length - pos;
}

print(count);

// total running time is about NlogN

- fiddler.g June 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Seems fine to me. Just that u need to insert the element every time to keep it sorted. Maybe costly in large arrays. I had given a elegant solution. The hint was i could use any data structure i want.

- Software Engineer June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The DS for the sortedArray should be List.

- fiddler.g June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Helps in faster insertion but still you have to do a compare linearly. And every time you insert a number in a large list you have to compare a lot. See if you can think of something else.

- Software Engineer June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The complexity of each insertion is log(current length of sortedArray). Use binary-search for insertion. :)
complexity =
log1 + log2 + ... + logN = logN! < NlogN

- fiddler.g June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Binary search on a list?

- Software Engineer June 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@fiddler: Your complexity computation is WRONG. Your approach is basically known as "Insertion Sort" - O(n^2) time algorithm.

Brush up your data structure basics. Do you know PROS and CONS of ARRAY vs LIST? I doubt!

- anonymous June 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Read the input from left to right, and keep inserting numbers in a ht balanced binary tree....O(nlog(n))
Now while doing insertions, every time you go left(i.e. number to be inserted is less than the current number/node) increment a global counter...
Return the global counter

- puneet15 June 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i guess you mean Binary Search Tree.
Okie good direction, but still its not the exact answer.

Try your solution for 8 9 10 7

- Software Engineer June 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah right, good observation.
I think we should also keep the size of right subtree in each node....then we can use the approach that when you are going left you found so many entries to be greater than the current number....and update the global counter according to that.
So in the given example:
first insert 8, counter = 0;
then insert 9, again counter = 0; because 9 is right child of 8.
then insert 10, again counter = 0;
but now tree restructures itself to mantain height balance...so now '9' is at the root...and it has right subtree of size 1...
so now when u insert '7' you go left at '9' and at '8', which correctly gives updates counter to 3

- puneet15 June 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cool, just that you could have made it even more simpler.
Keep a global counter and keep an internal node counter which keeps the count of the right subtree.
When you move right, increment the internal counter of the current node you are checking against, when you move left increment the global counter by one AND by the number in the internal counter.


So in the example i gave, insert 8, GC=0, insert 9, GC=0, internal counter of 8=1, insert 10, GC=0 internal counter of 8=2, and internal counter of 9=1.

now when you insert 7, increment global counter by one and add 2 to it since 8's internal counter is 2, that means there are 2 more numbers greater than 7.

So we get 3 as the answer.

Hope this is clear

- Software Engineer June 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are you guys (Puneet and SE) missing something ? The problem itself says that we have to find those numbers where B COMES AFTER A IN THE ARRAY. So the approach with BST should solve the problem, do let me know in case I am missing something here.

- gurha.pallav June 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, so thats correct. Using a BST should solve the problem efficiently. But we do need to store the size of right subtree, with each node. SE is doing that through internal counter.

- puneet15 June 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not sure if i got it correctly.
Lets take an example {3, 2, 1}
3 GC=0, IC (3)=0
3, 2 GC=1 IC(3)=0, IC(2)=0
3, 2, 1 GC=2, IC(3)=0, IC(2)=0, IC(1)=0

Ans =GC=2, but it is incorrect..answer should be 3 and the pairs would be {3,2}, {3,1}, {2,1}

Did i interpret the solution incorrectly?

- pkm June 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@pkm
you didn't.. because you traverse 3 left paths.
3 GC=0, IC (3)=0
3, 2 GC=1 IC(3)=0, IC(2)=0
3, 2 GC=2, IC(3)=0, IC(2)=0, IC(1)=0
2, 1 GC=3, IC(3)=0, IC(2)=0, IC(1)=0

- Sam October 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Good Work SE. Nice question and good soution..

- Musheka June 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose the Array = {N+1, N, N-1, ..., 10, 9 , 8, 7, 6, 5, 4, 3, 2, 1}
there are N(N+1)/2 array. to print these array need O(N^2);
So, the worst case to find these pairs is still equal to O(N^2).

- Guest June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes agreed, but this can be handled using Red Black trees, everything remains the same just that you need to recalculate the internal counter of each node when you do left or right rotation.

- Software Engineer June 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not sure if i got it correctly.
Lets take an example {3, 2, 1}
3 GC=0, IC (3)=0
3, 2 GC=1 IC(3)=0, IC(2)=0
3, 2, 1 GC=2, IC(3)=0, IC(2)=0, IC(1)=0

Ans =GC=2, but it is incorrect..answer should be 3 and the pairs would be {3,2}, {3,1}, {2,1}

Did i interpret the solution incorrectly?

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

I think I got it. Please correct me if i am wrong.
GC gets updated at each node if it goes in the left child. So in the above example
3 IC(3)=0, GC=0
3, 2 IC(3)=0, IC (2)=0, GC=1 (gets updated at 3)
3, 2, 1 IC(3)=IC(2)=IC(1)=0
GC (at 3 gets updated)=2, GC(at 2 gets updated)=3

Answer = 3 (which is correct)

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

when you are inserting 1, you take left two times(first for '3' and then for '2'), so GC is incremented twice..

- Anonymous June 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't understand why people LIKE to think a simple problem in complicated manner, like RB tree/other balanced tree which is pretty hard to implement for such simple problem!

It's a simple merging technique (like merge sort as pointed out by someone on June 20 post). This problem is taken from Cormen exercise (2-4).

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

An illustrative solution is given here:
geeksforgeeks.org/?p=3968

- dynamic July 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cant we solve it this way:
Begin creating a tree. In the beginning, count is zero. Every time a left path is chosen, increase the count and that becomes the answer.

- endeav.rd July 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cant we solve it this way:
Begin creating a BST tree. In the beginning, count is zero. Every time a left path is chosen, increase the count and that becomes the answer.

- endeav.rd July 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. RB tree is simple in Java.
2. only the number of pair is required

my solution in java is simple. O(nlgn) for sorting.

public void treeSolution(int[] orig) {
        TreeMap<Integer, Integer> tree = new TreeMap<Integer, Integer>();
        int[] pos = new int[orig.length];
        for (int i = 0; i < orig.length; i++){
            tree.put(orig[i], i);
        }
        int i = 0;
        for (Map.Entry<Integer, Integer> entry : tree.entrySet()) {
            pos[i++] = entry.getValue();
        }

        int count = 0;
        for (int j : pos){
            if (pos[j] > j) {
                count += pos[j] - j;
            }
        }
        System.out.println("total count: " + count);

    }

- amigobot July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The count is nothing but called conversion is an array. Consider them the number of pairs which needs to be swapped to make the array sorted. There is a modified version of merge sort to do this in O (nlogn) time.Its one of the back problem of Cormen. We did it in our algo class.

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

You can use the dayastructure called Binary Index Tree
It has a very simple 6 line implementation in C/C++.
And this problem in counting the number of inversions.

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

Just sort the array in increasing order and then binary search for each element. This way you could find the number of element greater than a given element in logn time. Do this for all n elements.
So sorting takes O(nlogn) time
n binary search takes O(nlogn) time
so overall time complexity is O(nlogn)

- Anonymous November 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Just count the number of inversions.. there's a O(nlogn) solution for that, using divide and conquer.

- DeathEater November 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

mergesort is ok~~~

- liyongbao1988 November 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

hi I think its a variation of longest in/decreasing subsequence....

- netappreject June 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

No it is not a variation of longest increasing subsequence instead as pointed out earlier, this requires just a slight modification of the merge method in merge sort.

Our purpose when we compare now is to see what elements are "out of order" so to speak and output them.. we dont want to sort them. This will take nlogn.

- Sid June 21, 2010 | 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