Amazon Interview Question for Software Engineer in Tests






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

just take two pointers,say p and q
p=0,q=n-1,count=0
while(p<q)
{
if(arr[p]+arr[q]==s)
{
p++;
q--;
count++;
}
if(arr[p]+arr[q]>s)
q--;
if(arr[p]+arr[q]<s)
p++;
}
cout<<count

- amd August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

that will result in n+ nlogn since you would have to sort the array first .... to get O(n) you would have to put it into hash table by checking the key value of sum-a[i] if it's there then pair foun else add it into the hash table.

- roy August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

roy's answer is correct that also satisfies the required complexity of O(n)

- googler August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Roy's solution assumes that the numbers are arranged in ascending order.
The question doesn't say anything about it.

- Balaji August 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No i don't assume that they would be in ascending order ... ...be the array in any order if you traverse through the array and check if the key (sum-a[i]) exists means their is a corresponding pair else put the value in hash... remember you don't have to worry in what order the hash is arranging it.. and the lookup is always O(1) .. Point to note i didn't say Array[sum - Array[i]] this would be a C/C++ implementation without any STL hash table. and it would require array to be sorted, but with hash table in JAVA or STL you won't need to sort it.

Anyways the interviewer told me the solution was rite at the end of the interview.

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

I'm just trying to learn stuff here. That's all.
The lookup is not O(1) rather O(n), for a hash table considering that collision is always possible. So, you'd spend essentially O(n2) time in solving the problem. Please correct me if I'm wrong.

- Balaji August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Everyone is learning, why else anyone be here. Anyways my point was yes look up for N number in hasp is O(N) but for one lookup it's O(1) ... there can be collisions but that doesn't effect us. Ex if there are two 4's and the sum you require is 7 so 4+3 = 7 which 4 doesn't make any difference. The question doesn't say which 4 makes that pair. if in an array there were 10 number of 4's and one 3 still the answer has to be 4,3.

O(n^2) means u compared each element with every other element in the array but when you use hash table you are comparing only once every element ... one element of array compared to one element of hash. so the total number of comparison equals n ... which means O(n)... don't even bother thinking how hash table gets the key value in O(1)... until and unless u really want to.

I hope this clears your doubt.

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

Hi, I am not quite sure that i understand. Let me give it my understanding out of this.

We have a set of numbers {a1,a2,a3......an} and a number S = ai+aj
Lets have a hash table of size n elements
Start iterating from k= 1 to n,
1. Fill the hash table with two entries, S - ak and ak
2. If one collision found out of two insertion above, let say ak (WLOG) Declare ak and S-ak as two numbers in the array which constitute the sum.
Else continue

Help me know the pitfall of this.

- Ankush Bindlish August 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http://techpuzzl.wordpress.com/2009/08/26/find-two-numbers-in-an-array-whose-sum-x/

- Geek August 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http://techpuzzl.wordpress.com/2009/08/26/find-two-numbers-in-an-array-whose-sum-x/

- Geek August 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks Geek

- hint February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Geek don't spam

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

This isn't spam, this has the answer. He probably posted twice because this site doesn't confirm when a comment has been posted

- shiggg May 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming array A and a number b, take another array C where C[i] = b - A[i]. ----> O(n)
Add elements in A into a hash table ----> O(n)
traverse through array C to see if any of elements is contained in the hash table ---> O(n)

This should be O(n)

- bohanl January 27, 2013 | 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