Amazon Interview Question for Software Engineer / Developers






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

//m is length of array a & n is length of  array b
public int[] merge_sort(int[] a,int[] b, int m, int n)
{
	a = Arrays.copyOf(a,a.length+b.length);
	int i = m-1;
	int j = n-1;
	int k = m+n-1;

	while(i>=0 && j>=0)
	{
		if(a[i] > b[j])
			a[k--] = a[i--];
		else
			a[k--] = b[j];
	}
	while(j>=0)
	{
		a[k--] = b[j];
	}
	
	return a;
}

- Algorithmist June 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice work!

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

var arrayB = new int[] { 1, 3, 5, 7, 10 };
var arrayA = new int[] { 0, 1, 2, 6, 8, 12, 13, 14,15 };

var finalArray = new int[arrayA.Length + arrayB.Length];

int indexA = 0;
int indexB = 0;


int index = 0;
while(indexA < arrayA.Length)
{
while(indexB < arrayB.Length)
{
int elementA = arrayA[indexA];
int elementB = arrayB[indexB];

if(elementA <= elementB)
{
finalArray[index] = elementA;
index++;
indexA++;
break;
}else
{
finalArray[index] = elementB;
index++;
indexB++;
continue;

}
}

if(indexB == arrayB.Length)
{
for(int i= indexA ;i< arrayA.Length; i++)
{
finalArray[index] = arrayA[i];
index++;
}
break;
}

}

- foobar June 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a cleaned up version of answer 1.

public int[] merge(int[] a, int[] b) {
  int i = 0;
  int j = 0;
  int index = 0;
  int[] result = new int[a.length + b.length);
  while (i < a.length && j < b.length) {
    if (a[i] <= b[j]) {
      result[index] = a[i];
      i++;
    } else {
      result[index] = b[j];
      j++;
    }
    index++;
  }
  //only one of the below for loops will execute, depending on if i or j exceeded
  //the bounds of a or b (respectively)
  for(; i < a.length; i++) {
    result[index] = a[i];
    index++;
  }
  for (; j < b.length; j++) {
    result[index] = b[j];
    index++;
  }
  return result;
}

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

public int[] mergeArray(int A[], int []B){
int []C =new int[A.length+B.length];
int indexA=0, indexB=0,indexC=0;
while(indexC <C.length){
if(indexA <A.length && A[indexA] <= B[indexB])
{
C[indexC]=A[indexA];
indexA++;
}
else if(indexB < B.length){
C[indexC]=B[indexB];
indexB++;
}
indexC++;
}

return C;
}

- Avinash Jha June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int A[];
int B[];
int C[];

int i=0;j=0;
while(i<A.length && j<B.length)
{
 if(A[i]<=B[j])
    C[k]=A[i]
    k++;
    i++;
 else
    C[k]=B[j];
    k++;
    j++;
}
while(i<A.length)
 C[k]=A[i];
 i++;
 k++;
while(j<B.length)
 C[k]=B[j];
 k++;j++;

- Mr.4342 July 13, 2011 | 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