Amazon Interview Question for Software Engineer / Developers






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

Sort, maintain two pointers one at left end and one at right. Move them (standard solution). O(n log n).

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

1. hash the array which takes O(n) time
2. for each element in array (1 to N), lookup the number (x-array[index]) in the hash if found print the pair (a,b).

Total Time: O(n)+O(n)=~ O(n)

- Anonymous August 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually, in step (2) you haven't included the time taken to look through the hash table for (x-array[index]). I believe that's going to increase the time.

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous2: Hashtable has a constant time look up ! Get your basics right first...

- Anonymous December 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if using hash then might as well do in O(n) and not O(n)+O(n).
do hashing and look up at the same time
hash for a,
look up for (x-a) exists if it does, you have your pair of (a, x-a)

- n1k41| January 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A detailed explanation here:
tech-queries.blogspot.com/2010/12/ sum-of-2-nos-in-array-equal-to-given.html

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

remove space from the link given

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

A detailed explanation here:
tech-queries.blogspot.com/2010/12/sum-of-2-nos-in-array-equal-to-given.html

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

Hey!
Stop with that particular blog links: It has sub-optimal solutions to most problems.

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

While creating the hash , we only pick the values which are less than x always , once the processing of array is done , we have the hash with <n elements ,for element(a) in hash , find the another element ST , x-a which is b.. this will be faster..

- Vinay Ks August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am afraid your solution won't work in case of negative numbers. {-2,6,10} and x is 8

- ACP Pradyuman September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private List<Pair> findSum( final int[] arr , int sum ){
		
		final Map<Integer, Integer> elems = new HashMap<Integer, Integer>();
		
		// O(N)
		for( int value : arr ){
			
			Integer counter = elems.get(value);
			
			if( counter == null ){
				counter = Integer.valueOf(1);
			}
			else {
				counter += 1;
			}
			
			elems.put( value, counter );
		}
		
		final List<Pair> pairs = new ArrayList<Pair>(); 
		
		// O(N)
		for( int value : arr ){
			
			Integer curCounter = elems.get(value);
			elems.put( value, curCounter-1 );
			
			int neededValue = sum - value;
			
			Integer neededCounter = elems.get(neededValue);
			
			if( neededCounter != null && neededCounter > 0 ){
				pairs.add( new Pair(value, neededValue) );
				elems.put(neededValue, neededCounter-1);
			}
		}
		
		return pairs;	
	}

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

void printNumbersWithSum(const int arr[], const int size, const int sum) { 
    std::set<int> numbersRequired; 
    std::set<int> :: iterator iter ;

    for (int i = 0; i < size; i++) { 
        iter = numbersRequired.find(arr[i]); 
        if (iter != numbersRequired.end()) { 
            std::cout<<(sum - arr[i])<<" + "<<arr[i]<<"   = "<<sum<<std::endl; 
        }   
        else { 
            numbersRequired.insert(sum - arr[i]); 
        }   
    }   
}

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

// first we sort the array using O(nlogn) approach
// then use the following method...
// overall solution = nlogn + n = nlong

int arr[] = {-2,0,1,2,3,6,10,19,21,23,43};

int start=0, end = arr.length-1;
int x= 0;

while (start<end) {

if(arr[start]+arr[end]<x)
start++;
else if(arr[start]+arr[end]>x)
end--;
else if(arr[start]+arr[end]==x){
System.out.println("pair " + arr[start] + " " + arr[end]);
break;
}
}

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

its not necessary that your code will give the correct solution. let consider the given array as
{-1,-2,3,5,6,9,12,30,34,35} and find the pair for x = 3. Then your code won't work. So some more constraints should be there for the increment of start and decrement of end.

- raj August 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

change the condition in while loop as while(start <= 9 and end>=0)...Then the same solution works

- Sunil August 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the array first, and then check the sum form the two ends. Increment both ends towards center until all pairs are found.

- xbai@smu.edu August 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ask someone who knows it..... there should be someone right? c'mon, there shud be

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

private static void TwoNumbersSumIndex(int[] arr, int val)
{
Hashtable searchTbl = new Hashtable();
int lookFor = 0;
for(int i=0; i < arr.Length; i++)
{
searchTbl.Add(arr[i], i);
lookFor = val - arr[i];
if (searchTbl[lookFor] != null)
{
Console.WriteLine("Matching numbers - " + lookFor + " & " + arr[i]);
return;
}
}
}

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

Use a hash table,thatshould be linear
Or Hire 'N' people n solve in logN time

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

Sort and perform binary search for sum-a[i] for each element--O(nlogn)

- Anonymous August 27, 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