Google Interview Question for Software Engineer / Developers


Country: United States




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

Merge Sort can be tested for the possible cases:
1. Giving odd and even number of terms
2. Already sorted in increasing and decreasing order
3. Repeating the middle element
4. Same term repeated for all the ith terms
5. Same number of terms and the same terms in the output so as to check the merging os proper

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

Did he ask for in-place merging or was making copies of the merging arrays allowed?

In place merging is complicated ...

- Odi January 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

/ *** In Place Merge Sort ***/

public class MergeSort {
	
	public void mergeSort(int[] arr, int low, int high){
		if(high > low){
			int mid = (low + high) / 2;
			mergeSort(arr,low,mid);
			mergeSort(arr,mid+1,high);
			mergeInPlace(arr,low,mid,high);
		}
	}
	
	private void mergeInPlace(int[] arr, int low, int mid, int high) {
		int i = low, j = mid + 1, tempVar = 0;
		while(i <= mid && j <= high){
			if(arr[i] > arr[j]){
				tempVar = arr[i];
				arr[i] = arr[j];
				int newIndex = j;
				while(newIndex <= high && arr[newIndex+1] < tempVar){
					arr[newIndex] = arr[newIndex+1];
					newIndex++;
				}
				arr[newIndex] = tempVar;
				j++;
			}else{
				i++;
			}
			
		}
		for(int l=low; l<=high; l++){
			System.out.print(arr[l] + " ");
		}
		System.out.println();
	}


	public static void main(String[] args) {
		MergeSort mergeSort = new MergeSort();
		int[] arr = {1, 5, 3, 4, 6};
		mergeSort.mergeSort(arr, 0, arr.length-1);
		System.out.println(" ------------------- ");
		int[] arr_2 = {2, 9, 3, 3, 4, 8};
		mergeSort.mergeSort(arr_2, 0, arr_2.length-1);
	}
}

- Anonymous January 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

and we could also test the merge function in it seperately by passing it two sorted array and check if it returns sorted and merged array

- Ashu December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[] MergeSort(int[] arr, int[] arr2)
        {
            int[] result = new int[arr.Length + arr2.Length];

            int index = 0;
            int index2 = 0;
            for (int i = 0; i < result.Length; i++)
            {
                if (index == arr.Length)
	            {
		            for (int j = index2; j < arr2.Length; j++)
			        {
			            result[i++] = arr2[j];
			        }
                    break;
	            }

                if (index2 == arr2.Length)
	            {
		            for (int j = index; j < arr.Length; j++)
			        {
			            result[i++] = arr[j];
			        }
                    break;
	            }


                if (arr[index] > arr2[index2])
                {
                    result[i] = arr2[index2];
                    index2++;
                }
                else
                {
                    result[i] = arr[index];
                    index++;
                }
            }

            return result;
        }

- Quan Li April 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mergeSort (vector<int> & Arrary)
{
    int len = Arrary.length();
    vector<int> workArrary;
    for( int blockSize = 1; blockSize < len; blockSize *= 2)
    {
        for (int blockHead = 0; blockHead<len; blockHead += blockSize)
        {
            merge(Arrary, blockHead, blockSize, workArrary);
        }
    }
    Arrary = workArrary;
}
merge( vector<int> & Arrary, int blockHead, int blockSize, vector<int> & workArrary)
{
    int leftIndex = 0;
    int rightIndex = 0;
    for (int i = 0; i< 2 * blockSize; i++)
    {
        if ( leftIndex == blockSize)
        {
            workArrary[i] = Arrary[blockSize + (rightIndex++)];
            continue;
        }
        if (rightIndex == blockSize)
        {
            workArray[i] = Arrary[leftIndex++];
            continue;
        }
        workArrary = Arrary[leftIndex]<Arrary[rightIndex]? Arrary[leftIndex++]: Arrary[rightIndex++];
    }
}

/* test cases
* 0, 1, 0000000, 10101010,010101, 123456, 654321, -1 -2 -3 -4 -5,
*/

- lixulehigh September 27, 2012 | 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