Amazon Interview Question for Software Engineer / Developers






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

Sort. Traverse from either ends.

- knap February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hashing would work in O(n).

- Silence February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What hashing

- Sumeet February 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort O(nlogn)
now start from both ends

target_sum(int a[n],int target)
{
qsort(a);//sorting :P
int i,j;
i=0; j=n-1;
sum=0;
while(i<=j)
{
sum=a[i]+a[j];
if(sum==target) {cout<<a[i]; cout<a[j]; break;}
else if(sum<target) i++;
else if(sum>target) j--;
}
return
}

- Yoda February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hashing would work in the following way...
insert all the elements of the array in the hashtable..this takes O(n) time
now for each element a[i] in the array check whether targetNumber - a[i] exists in the hashtable. this takes O(n) time
totally TC = O(n+n) = O(n)..

- periaganesh February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the array. pick first number, subtract it from target and binary search for (Target-firstNum) in A[2...N], then pick A[2] and binary search for T-A[2] in A[3...N] and so on. Complexity. Sort=nlogn, find=n times logn. Total nlogn

- Anonymous February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As periaganesh told, hashing is the optimal approach to solve this problem

- sathiyan k April 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

depends,yea optimum in time complexity but hashing causes extra memory.

- Anonymous December 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MapTarget {
	public static void main(String...v){
		
		int[] arr={2,6,8,4,5,1,7};
		int target =15;
		Map<Integer,Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0; i < arr.length; i++) {
			 map.put(arr[i], arr[i]);
			}
		
		for (int i = 0; i < arr.length; i++) {
			int k= (int) map.get(arr[i]);
			 if(map.containsKey(target-k))
				 System.out.println(arr[i]);
			}
				
	}

}

- Arvind February 25, 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