Amazon Interview Question for Software Engineer / Developers






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

could be done in On

static void sumExists(int a[],int sum){
		Hashtable ht=new Hashtable();
		int tmp=0;
		int num1=0,num2=0;
		boolean exist=false;
		
		for (int i = 0; i < a.length; i++) {
			ht.put(i, i);
		}
		
		for (int i = 0; i < a.length; i++) {
			num1=a[i];
			tmp=sum-num1;
			if(ht.get(tmp)!=null){
				exist=true;
				num2=tmp;
				break;
			}
		}
		
		if(exist){
			System.out.println(num1+ "+"+num2+ " = "+sum);
		}
		else
			System.out.println("doest exist");
	}

- Omer February 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How do you resolve input array with dups?
Hashtable would bail out with dup keys.
You might have to add some special casing there.

- satishl November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You also have a problem with this input.
input: {1,2,3,4,5} , sum = 10.
this function will return 5,5 as the answer, where as the right answer is none.

- satishl November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@satishl: To solve the Duplicate elements problem, here's what can be done, to modify the hash table algo:
BEFORE adding an element 'a[i]' to hash, check if (Sum - a[i]) is already present in the Hash.
If it Exists, we have found the pair,
Else, just add this element a[i] to the hash.

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

sort the array (nlogn)
i=0, j=n-1 (j is set to last element of the array)

then use following algo ( O(n))

while i<j
if( (arr[i] + arr[j]) > val)
j--;
else if( (arr[i] + arr[j]) < val)
i++;
else
print arr[i], arr[j]

- Anonymous November 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Better time complexity can be achieved by using a hashtable

- swapnilkant11 July 21, 2019 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

sort the array (nlogn)
i=0, j=n-1 (j is set to last element of the array)

then use following algo ( O(n))

while i<j
if( (arr[i] + arr[j]) > val)
j--;
else if( (arr[i] + arr[j]) < val)
i++;
else
print arr[i], arr[j]

This greedy algorithm is quite good.

- Erik March 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This was my first thought, interviewer didn't like it much since its a nlogn. Shoot for fastest algo possible, which is O(n)

- JD March 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This sol wont work , if there are multiple pairs summing up to the given value.

- vivekh October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash table

O(n)

- Anonymous November 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+o(n) space complexity

- gagan November 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

No hash table
O(nlogn)

- rottentomato November 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How?? Can you please explain.

- Anonymous November 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I love it when people answer specific questions with data structures. Clearly shows they don't know what they are talking about. I have 7 solutions off the top of my head.

1.) Red black tree
2.) AVL tree
3.) Fibonnaci heap
4.) DAG
5.) DFA
6.) Stack
7.) Linear programming

- Anonymous November 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the point of discussing these questions is to get an idea of how other people would actually implement a solution. Supplying data structures and programming paradigms as answers does no good. It would be nice if you could provide an in depth explanation of the most efficient of your solutions.

- anon November 27, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Anonymous wrote:
--------
I love it when people answer specific questions with data structures. Clearly shows they don't know what they are talking about. I have 7 solutions off the top of my head.

1.) Red black tree
2.) AVL tree
3.) Fibonnaci heap
4.) DAG
5.) DFA
6.) Stack
7.) Linear programming

---------------

Idiot. What a silly thing to say. In fact you seem to quite adept at demonstrating your ignorance.

Just saying Hashtable is a perfectly reasonable answer.

Do you understand that people here are of their own will, sharing their time to help others out? So what if they don't include a complete solution, just a few keywords (in this case, hashtable) should be good enough for a determined person to come up with a solution for that.

- T March 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Stack???

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

1) Find the max, using a sort. nlog(n)
2) Populate a bit vector A[] with 1 for each integer in the array, 1 for those not in the array.
3) loop through the integer array, for each int i, if A[n-i]== 1. i and n-i are the two integers that add up to N.

There is no need to search in the 3rd step.

- Anonymous November 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find min and max in O(n). then define a hash table with hashing function h(num)=num%d, where d=(max-min)/n.
now, for each element in an array search if the hash table contain a value (sum-num[i]) [i.e. search at the index h(sum-num[i])], if yes the present index and the number stored in hash table is the pair. If not, insert into hash table num[i].
Using this function, if we can assume that the numbers are uniformaly distributed, we can say that each bin will have one number on an avg. so the searching will be done in O(1). therefore total complexity is O(n).

- Piyush December 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good Solution..

- Anonymous January 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Anonymous wrote...
> Good Solution.


Not really.

- T March 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution:
1. for each element in array add it to hashtable (HashSet in Java)
2. for each element in array, calculate sum-element, now lookup this value in the table. If it exists you're done

- JD March 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

---------
sort the array (nlogn)
i=0, j=n-1 (j is set to last element of the array)

then use following algo ( O(n))

while i<j
if( (arr[i] + arr[j]) > val)
j--;
else if( (arr[i] + arr[j]) < val)
i++;
else
print arr[i], arr[j]

This greedy algorithm is quite good.
-----------

This algo won't work. check it out.

- whatsinthename April 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And why won't it work...?

- T April 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This algo wont work, if the are multiple pairs which satisfy the sum condition.

- vivekh October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Find_Two_number(int a[],int size,int number)
{
	int max =  a[0];
	int x = -1,y = -1;
	for (int i = 0;i<size;i++)
	{
		if(a[i]>max)
		{
			max =  a[i];

		}

	}

bool *b =  new bool[max+1];

for (int i = 0;i<=max;i++)
{

b[i] = false;

}

for (int i = 0;i<size;i++)
{

b[a[i]] = true;

}




for (int i = 0;i<size/2;i++)
{

if(b[a[i]] == true)
{

	if(b[number- a[i]] == true)
	{
		cout<< "X= "<< a[i]<<"; Y = "<< number-a[i]<<endl;

	}


}

}


}

- Amit Agarwal January 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can be exponential in the size of the input !

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

Tell me if above sol dont work

- Amit Agarwal January 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes, it could be done in linear.. O(n).

public void PairsOfArraySumsUpToGivenInteger(int[] arr, int sum)
        {
            int i = 0, j = i + 1;

            while (i < arr.Length)
            {
                if (arr[i] + arr[j] == sum)
                {
                    Console.WriteLine(arr[i] + "," + arr[j]);
                    j++;
                }
                else
                    j++;

                if (j >= arr.Length)
                {
                    i++;
                    if (i < arr.Length - 1)
                        j = i + 1;
                    else
                        break;

                }

}

- Jeanclaude July 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hashing seems to be perfect method if we want to reduce time complexity. It does have a space complexity of O(n), but that's not a concern I guess.

- sid1505 September 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above algorithm can be solved using hashing, keep calculating the difference of the given number to each element in the array if the element is present in the array then, return true else insert the element into the hashset and when it is not true then, at the end of the operation return false.

Implemenatation:

#include<bits/stdc++.h>
using namespace std;
bool findsum(int arr[], int n, int x){
    unordered_set<int> s;
    for(int i = 0; i < n; i++){
        int diff = x - arr[i];
        if(s.find(diff) != s.end())
            return true;
        s.insert(arr[i]);
    }
    return false;
}
int main(){
    int t, n, x;
    cin>>t;
    while(t--){
        cin>>n>>x;
        int arr[n];
        for(int i = 0; i < n; i++)
            cin>>arr[i];
        if(findsum(arr, n, x) == 1)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
	//cout<<"Keep learning Keep growing"<<endl;
	return 0;

}

- swapnilkant11 July 21, 2019 | 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