Amazon Interview Question for Developer Program Engineers


Country: United States




Comment hidden because of low score. Click to expand.
7
of 11 vote

Hash the square of all elements.

Simply Use two loops to get all possible a^2 + b^2 combinations and check if c^2 is in hash. T

Time: O(n^2). Space: O(n)

- kill -9 March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would be depend on the good hash function. But average it would be good if we provide Kn space, k>1.

- chenlc626 March 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

Space Complexity will grow high for using hash table. Inefficient and not feasible solution. C^2 for each number has to be stored thus is not possible for large triplets.

- Praveen April 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If there are 3,4,5,-5, then it ends up with 2 combinations 3,4,5 and 3,4,-5. How is this handled if the integers are squared and hashed

- Anonymous May 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

We need to sort the array to be able to achieve O(n^2) performance.

void findTriplets(int *input, int size)
{
int a, b, c, i;

for (c = 0; c < size; c++)
{
int result = input[c] * input[c];
a = 0;
b = size - 1;
while (a <= b)
{
if ((input[a] * input[a]) + input[b] * input[b]) < result) /* a^2 + b^2 < c^2 */
a++;
else if (input[a] * input[a]) + input[b] * input[b]) > result) /* a^2 + b^2 > c^2 */
b--;
else
printf("triplet found: %d %d %d \n", input[a], input[b], input[c]);
}
}
}
}}}
Time: O(n^2) Space: O(1)

- kill -9 March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

small correction needed..
here
b=c-1; and c has to start from size-1 to 0;
since a^2+b^2=c^2 ... only when c>a &c>b;

- bharat April 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

But the element in the array may be negative. Sorting it is not reasonable

- terry April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Since If we will square any negative number then it will become positive number. So we can change all negative number to +ve it will take O(n) time complexity. After that sort array which will take nlogn. Now in end hold first number and start iteration from i = 0 to i = n-1 and j =n-1 to j= 0 where i must be less than j. In this way we can find triplets.

Note :- It will give solution in O(n*n) time complexity and O(1) space complexity if interviewer is allowing you to change array other wise you can modify quick sort code and instead of sorting it on the basis of a[i] we can use abs(a[i]).

- anonymous March 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

give solution better than n^2 log n

- goelrinku90 March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) First square all the numbers.
2) Sort the Array
3) Store this array element in a Hashset.
4)Open a for loop , which loops from starting element of the array.
5) open another for loop, which loops from next to starting element of array.
6) Add these two elements and check whether the sum is present in HashSet, if true break else continue.
7) By using HastSet we can reduce the time complexity for searching the result.

- Prashanth April 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This looks like a great solution and the steps are easy to follow. However, I believe the question asks to find pythagorean triplets -- not to just find one solution and break.

- Anonymous March 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Example: When n = 2, the triple produced is 5, 12, and 13. (This formula is actually the same as method I, substituting m with 2n + 1.)
check : en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples

- Anonymous April 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

More formally: given a positive integer n, the triple can be generated by the following two procedures: (see mcs.surrey.ac.uk/Personal/R.Knott/Pythag/pythag.html )

a = 2n+1, b = 2n(n+1), c = 2n(n+1)+1

Example: When n = 2, the triple produced is 5, 12, and 13. (This formula is actually the same as method I, substituting m with 2n + 1.)

Alternatively, one can generate triples from even integers using the following formulas. Given that m is a positive even number,

a = 2m, b = m^2-1, c = m^2+1

Example: When m = 4, the triple produced is 8, 15, and 17 (this formula is another specific case of method I, substituting n with 1).

- Anonymous April 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can do it in O(nLogn) [sort]+n Logn[find corresponding numbers] after sorting and using above formula.

- King@Work April 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't think m has to be even for the 2nd formula,
for m = 3, we will get a = 6, b= 8, c=10 which is a valid pythagorean triplet

- Anonymous April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(int i=0;i<N;i++) aux[i] = a[i]*a[i]; // O(N) 

quick_sort(aux); // O(N*log(N)

for(int i=0;i<N;i++) for(int j=0;j<i;j++) 
 binary_search(aux[i]+aux[j], aux); //  O(N^2*log(N))

Time complexity N^2*log(N), no extra space

- vkapko February 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

actually, we can achieve o(n*n) at least for time complexity and O(1) for space complexity without HASH. We first need to change the value to its square one with its original sign, for example, 3=>9, -3=>-9. Then sort them using the fast sort according to the absolute value of the element, abs(9)=9, abs(-9)=9 in nlogn time complexity. then we are going to find the triple, using A+B=C, A=abs(a), B=abs(b), C=abs(c), a,b,c is the element of the square value. For each fixed C value, we know can search the pair A+B =C in O(n) time, so overall we can have O(n*n) time complexity for the case. if we choose the in place quick sort algorithm, the space complexity is O(1).

- chenlc626 March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In-place quicksort has logarithmic space complexity.

If you replace each integer by its square, it could overflow. For k-bit integers, you could need up to 2k bits to store the square of that number. Basically, if you were given an array of ints, in the worst case, you'd have to make an array of longs to hold the squares.

- eugene.yarovoi March 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hmm, right, i mis-calculate the space complexity for the quick sort, ;), should be the O(logN) in average. Yes, square may need one more bit for each int, but it may reduce some computation effort, which otherwise will need to computated in O(n*n).

- chenlc626 April 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

To find the pythagorean triplet.

A^2 + B^2 = C^2
You can simplify both the equation for using two variables.

A = n^2 - m^2;
B = 2nm;
C = n^2 + m^2;
for all n> m.

Now in the for loop print (A,B,C) for every pair of numbers by using above function.

Time Complexity O(n) Space O(1);

- Praveen April 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Time Complexity O(n^2)! not O(n)

- Praveen April 02, 2013 | Flag


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