Amazon Interview Question for Software Engineer in Tests


Team: Chennai
Country: India
Interview Type: Phone Interview




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

Since you have to traverse the entire array atleast once, Time Complexity : O(n)

public static void main(String args[]){
		int arr[] = {2,1,4,6,7,8,70,69};
		for(int i=1;i<arr.length;i++){
			if(Math.abs(arr[i] - arr[i-1])==1){
				System.out.println("["+arr[i]+","+arr[i-1]+"]");
			}
		}
	}

- Anonymous January 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a[LENGTH];

for(i=0; i<LENGTH;i++)
{
if( 1== abs(a[i]-a[i+1]))
printf("%d %d",a[i],a[i+1]);
else
continue;
}

Time Copmplexity = O(n)

- Rajesh Manem January 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Rajesh - always test your code before posting. Your code will through index out of range error when i reaches length-1

- siva January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

People have got the question wrong, I think.

The array could be {4,7,6,9,3}

4 and 3 are adjacent with difference 1.

For this version, use a hashtable, and given a[i], check if a[i] -1 and a[i]+1 are already there.

- Anonymous January 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question doesn't mention a 'circular' array!
Even if the array were circular, the O(N) solution can easily be fixed by extending the indices (and proper index mod) in the loop or an extra step outside the loop to check for the last-first element pair.
Using a hashtable for this problem appears, to me at least, to be a horrible idea.

- Shams January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No shams. You misunderstand the intrepreation of this question.

The difference 1 is defining what adjacent means.

They are not looking for a[i] and a[i+1] such that |a[i] - a[i+1]| = 1.

They are looking for i and j such that |a[i] - a[j]| = 1. For this version, hashtable is perfect.

Otherwise the problem is utterly trivial.

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

#include<stdio.h>
#include<conio.h>
main()
{
	int temp=0;
	int arr[]={-13,-12,18,17,19,20,23};
	int length=sizeof(arr)/sizeof(arr[0]);
	for(int i=0;i<length;i++)
	{
		temp=arr[i];
		temp+=1;
		if(temp==arr[i+1])
		{
			printf("Elements are {%d,%d} and at position{%d,%d}\n",arr[i],arr[i+1],i,i+1);
		}
	}
}

- Shakya January 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
int i , j, a[20], n;

printf("entr the size of array ");
scanf("%d", &n);
printf("enter the element");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);

}

for(i=0;i<n;i++)
{
j=i+1;
if((a[i]-a[j]==1)||(a[j]-a[i]==1))
{
printf("%d%d",a[i],a[j]) ;
}
else
printf(" there are such pairs of elements whose difference is equal to1 ");
}
getch();
}

- sony kumari January 26, 2014 | 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