Amazon Interview Question for Software Engineer in Tests






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

This is a variation to question 9626448.

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

ok
just take two integers i and j which points to last index of array wher value exist means
at n-1
and j should be also n-1
k should be assined as 2*n-1
now start compairing value at these if b[i]>a[j] then just do b[k--]=b[i--];
in else
do b[k--]=a[j--];
do till u not have k!=0

- geeks July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Kool approach

- Rahul Sharma July 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is array sorted (Not mentioned in this question)

- Saumya July 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, My Bad....

- Saumya July 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will work only will n elements are followed by n vacant spaces .However in question its n elements and n vacant spaces ,what if the n vacant spaces and n elements randomly arranged.

- Ankit July 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome

- gautam142857 March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Copy b's elements to the second half of b. Now just merge on b.

- memo July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bubble up the vacant spaces to the end, and start merging from the end of arrays.

- harshal ^-^ July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we use extra space?

- rocky July 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey39953" class="run-this">#include<stdio.h>
#include<conio.h>

int main()
{
int a[5],b[10],i,j,n=5,k;

printf("a :");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("b :");
for(i=0;i<5;i++)
scanf("%d",&b[i]);


for(i=0,j=0;i<n && j<n;)
{
if(a[i]<b[j])
{
for(k=n-1;k>=j;k--)
b[k+1]=b[k];
b[k+1]=a[i];
i++;
j++;
n+=1;
}
else
j++;
}
for(i=0;i<n;i++)
printf("%d ",b[i]);
getch();
return 0;
}


</pre>

- Anonymous July 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think this pgm complexity is not o(n). . it may take o(n log n)

- gayu July 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) is easily possible.

A[2n] shift all elements to A[n] making A[n+1] A[2n] vacant and then merge.

Two loops O(2n) + O(2n) = O(n)... Wondering if can be done in less.

- WellSaid August 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first put all the elements in b[] to the left most side or the array leaving n spaces empty on the right. O(n) use 2 pointers..
then start the pointers from a[n-1] and b[n-1] and another pointer where you will be filling either of these 2 depending which is bigger. this pointer will be at b[2n-1]. O(n) for a[] and O(n) for b[].. therefore O(3n) = O(n)

- r August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Move all elements in big array to end keeping order same in O(n+n)
2. Merge 2 sorted contiguous arrays in big array. index1 = 0 index2 = n: O(m+n)

ME()
	k = m+n-1
	for each i from 0 to m+n-1
		if(b[i] != -1)
			swap(i,k)
			k--

M()
	i = 0
	j = n
	k = 0
	while(i < n ||  j< n+m)
		if(s[i] <= b[j])
			b[k++] = s[i++]
		else
			b[k++] = b[j++]
	if(j < (m+n))
		while(j < (m+n))
			b[k++] = s[j++]
		

MA()
	ME(b)
	M()

- Prateek Caire November 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction

ME()
	k = m+n-1
	for each i from m+n-1 to 0

- Prateek Caire November 08, 2011 | 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