Amazon Interview Question for Software Engineer / Developers






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

Why hashing method can not handle negative numbers?

- Hrishikesh May 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How does the hashing method work?

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

Step 1: Sort --> O(nlogn)

Step 2: assuming A is array of size n,
let i=0 and j=n-1
if A[i]+A[j] > x then j--
else if A[i]+A[j] < x then i++
if equal then return i,j
repeat this till i<j --> O(n)

total complexity: O(nlogn)

- Kartik May 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone explain this ? How this works ?
>>>> 2. Uisng sorting as a preprocessing step and binary search. ie. O(nlogn).

- Anonymous May 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We sort the array first O(n)
For each integer, we do binary search for sum-integer in the remaining array, which requires O(log(n))
Therefore, the total is O(nlog(n)).

accu rp

- chenming831@hotmail.com May 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We cannot do the sorting in O(n) time complexity.

- Anonymous May 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, but we can do list traversal in O(n) and sorting in O(nlogn). Since O(nlogn) >> O(n) we can say it's O(nlogn).

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

It should be able to handle negative numbers just fine.
Some pseudocode..

Let the number we're trying to sum upto be SUM

Step 1.
foreach(int n in array)
{
	int complement = SUM - n;
	if(hashTable.containsKey(complement)
		return true; // found the pair {n, complement}

	hashTable.Add(n, complement); 

// You can simply choose to add true or 1 here. I chose to add the complement value as the
// value part of the key,value pair thing.
}

Step2. There is no step 2.

It should work wiht -ve numbers just fine

for example

a  = [2, -2, 5, 6, 9, -14, -3]
sum = 3

hashtable[-2] = 5
so -2, and 5 will be a valid pair

- PrateekS. August 03, 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