Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

private static int[] mergeArray(int[] array1, int[] array2) {
// TODO Auto-generated method stub
int[] output = new int[array1.length+array2.length];
int i=0,j=0 ,k=0;
while(i< array1.length && j<array2.length )
{
if(array1[i]<array2[j]){
output[k++]=array1[i++];
}
else{
output[k++]=array2[j++];
}
}
if(array1.length -1 != i){
while(i<array1.length){
output[k++]=array1[i++];
}
}
if(array2.length -1 != j){
while(j<array2.length){
output[k++]=array2[j++];
}
}
return output;
}

- MVVSK December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You could have put contents of both arrays in a bigger array and done bubble sort.

- suvrokroy December 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code will not produce the sorted array as output.
http: //ideone.com/2MgvXS

- Jayanth December 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@suvrokroy : there are two problems if we in your approach ..
1) we can not increase the size of int array dynamically , so we can not put all the content to big array ...
2)If we want to use bubble sort we need to create a new array with content of both array , the complexity will be
N: length of first array
M length of second array
time : O((N+M)2) = O(n2)
space : O(M+N) = O(n)

The complexity of my solution is
time : O(M+N) = O(n)
space : O(M+N) = O(n)

- MVVSK December 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Jayanth : i have copied the executing code , it is working .
The code in link u have pointed is not proper , there are some differences between codes have a look at it .

- MVVSK December 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1.The two arrays are fixed not dynamic so why do we need a dynamically growing array?Big array=array 1 + array 2

2.Why do I need a third array?I will do bubble sort with the Big array.

Please let me know if I am wrong

- suvrokroy December 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am assuming the initial two arrays are also sorted:

private static int merge(int[] longArray, int[] shortArray, int longUsed) {
        int longTail = longUsed - 1;
        int shortTail = shortArray.length - 1;
        while (longTail >= 0 && shortTail >= 0) {
            if (longArray[longTail] > shortArray[shortTail]) {
                longArray[longTail + shortTail + 1] = longArray[longTail];
                longTail--;
            } else {
                longArray[longTail + shortTail + 1] = shortArray[shortTail];
                shortTail--;
            }
        }
        while (shortTail >= 0) {
            longArray[shortTail] = shortArray[shortTail];
            shortTail--;
        }
        return shortArray.length + longUsed;
    }

- BoredCoder December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we assume that arrays are sorted already?

- Mike1324 December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

yes two sorted arrays merge to get sorted array

int[] merge(int[] a, int[] b){
int[] answer = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
{
if (a[i] < b[j])
{
answer[k] = a[i];
i++;
}
else
{
answer[k] = b[j];
j++;
}
k++;
}

while (i < a.length)
{
answer[k] = a[i];
i++;
k++;
}

while (j < b.length)
{
answer[k] = b[j];
j++;
k++;
}

return answer;
}

and runtime complexity is o(m+n) m, n are array sizes

- Minnu December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Given two input arrays are sorted ??

- avikodak December 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this can be done in O(logn)

- sophia December 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Array merge(Array A1, Array A2) {
    int size = A1.size+A2.size, i=0, a1i=0, a2i=0;
    Array MA(size);
    A1.sort();
    A2.sort();
    whiile(a1i<A1.size && a2i<A2.size){
        if(A1[a1i]<=A2[a21])
            MA[i++] = A1[a1i++];
        else
            MA[i++] = A1[a2i++];
    }
    if(a1i<A1.size){
        while(i<size){
            MA[i++] = A1[a1i++];
        }
    } else if(a2i<A2.size){
        while(i<size){
            MA[i++] = A1[a2i++];
        }
    }
    return MA;
}

- SRB December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone Explain why we are using:
if(array1.length -1 != i){
while(i<array1.length){
output[k++]=array1[i++];
}
}
if(array2.length -1 != j){
while(j<array2.length){
output[k++]=array2[j++];
}
}

- Anuj December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You dont need that condition

- kary December 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Collections;
import java.util.ArrayList;
import java.util.Arrays;

class MergeArrays {
  public static void main(String[] args) {
    int[] array1 = {10,9,8,7,6};
    int[] array2 = {3,4,5,9};

    ArrayList<Integer> arrayList = new ArrayList<Integer>(array1.length + array2.length);
    for(int i=0; i< array1.length; i++) {
      arrayList.add(Integer.valueOf(array1[i]));
    }

    for(int i=0; i< array2.length; i++) {
      arrayList.add(Integer.valueOf(array2[i]));
    }


    Collections.sort(arrayList);

    int[] mergedSortedArray = new int[arrayList.size()];

    for (int i=0; i<arrayList.size(); i++) {
      mergedSortedArray[i] = arrayList.get(i).intValue();
    }

    System.out.println("Resulting array: " + Arrays.toString(mergedSortedArray));
  }
}

- Sharma December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Start from the end of both the arrays... typical 0(m+n) solution...

- Rahul Arakeri December 14, 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