Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Reversing an integer array:

void reverse(int a[],int len){
int i,temp;
for(i=0;i<len/2/;i++){
temp = a[i];
a[i] = a[len-i];
a[len-1] = temp;
}
}

- rkt March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is it true

- sasa March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yeah I'll give it a shot. For Java int array:

public class Reverse
{
	public static void reverse(int[] data)
	{
		int length = data.length;
		for(int i = 0; i < data.length/2; i++)
		{
			int temp = data[i];
			data[i] = data[data.length-1-i];
			data[data.length-1-i] = temp;
		}
	}
}

- movingincircles March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

hey put it in a stack and retrieve it..i think that would do the job.

- vishwanath March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why do you need to add to the space complexity?

- RV March 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

but it certainly reduces time complexity right?

- vishwanath March 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, it does not...
If you iterate with two pointers from front and back...loop will run len/2 at max...even if you consider 2 extra statements for swapping it will be "n".
But in your case we need 2n iterations one for putting all elements in stack another for getting all elements back to array.....since array provides O(1) lookup time you should make use of it....your implementation is a good solution for linked lists.

- Anshul March 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@rkt -- here your code is wrong for some i/p ex-{1,2,3,4} when u iterate this i/p u find an error.....
correct code is.....
// here len of the array means no of element in that array.....

void reverse(int a[],int len){
int i,temp;
if(len%2==0)
{
len =len-1;
for(i=0;i<(len/2)+1;i++){
temp = a[i];
a[i] = a[len-i];
a[len-i] = temp;
}
}
else
{

for(i=0;i<len/2;i++){
temp = a[i];
a[i] = a[len-1-i];
a[len-1-i] = temp;
}
}


}

- raunak.gupta29 March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

#include<stdio.h>
#include<stdlib.h>

void print(int *arr, int len) {
	int i;
	for (i=0;i<len;i++)
		printf("%d, ", arr[i]);
	printf("\n");
}

void swap(int *a, int *b) {
	int tmp=*a;
	*a=*b;
	*b=tmp;
}

void reverse(int *arr, int len) {
	int i=0;
	while (i < len--)
		swap(&arr[i++], &arr[len]);
}

int main(void) {
	int *arr=NULL;
	int size, i;
	printf("Enter array size : ");
	scanf("%d",&size);
	arr=(int *)calloc(size,sizeof(int));
	for(i=0;i<size;i++)
		scanf("%d",&arr[i]);
	print(arr,size);
	reverse(arr,size);
	print(arr,size);
	free(arr);
	return 0;
}

- puri March 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