Amazon Interview Question for Developer Program Engineers






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

#include <iostream>
using namespace std;

int main()
{
    int a[] = {0,0,1,1,0,1};
    int b[] = {1,0,1,0,0,0};

    int n = sizeof(a)/ sizeof(a[0]);
    int p = 0, q = 0, sumA = 0, sumB = 0, sum = 0;

    for(int k = 0; k < n-1; k++)
    {
     for (int j = n-1; j > 0; j--)
       {
           sumA = 0; sumB = 0;
    
        for(int i = k; i <= j; i++)
         { 
           sumA += a[i];
           sumB += b[i];
           }
        if(sumA == sumB && j > k && (q-p) < (j-k))
         {
           p = k; q = j; sum = sumA;
           }
        }
 }

 cout << p << " " << " "  << q << "  " << sum << endl;
    
 return 0;
 }

- JoshMachine November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is brute force approach .. Can it be done in better than O(n^3) ??

- JoshMachine November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

can be done in O(n*n) comfortably
But interview is definately looking for better than O(n*n)

- nitin November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution is O(N). It is the Longest Common Substring with a modification. So only one loop

- Narendran November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you got confused... the strings are not the same in this case. The sum of values is same.
100101 == 110001 in this case because number of bits set = 3 in both cases
longest common substring requires exactly same strings

- chennavarri November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are n*(n+1)/2 intervals that can be formed using various combinations of p and q for a single array. Which means there are n*(n+1)/2 elements. Now the question is,
if K = n*(n+1)/2 can you design an algorithm that will find the max from K in less than K comparisons. Does that make sense? finding a max of an array of size K in sublinear time. How? I dont see a solution.
The best solution I can see is O(n+2+3+4+..(n-1)) => O(n*(n+1)/2) = O(n^2)

Solution:
Since, all the values are binary, all we need is count the number of bits set. So.
Count all the bits set in both arrays, totalSumA, totalSumB
when p - q == n-1, we do 1 comparison (totalSumA==totalSumB) ??
when p - q == n-2, we do 2 comparisons //just subtract the ends from totalSumA and totalSumB and compare
when p - q == n-i , we do i comparisons
=> total number of comparisons required = n*(n+1)/2
=> O(n^2)

- chennavarri November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

form a new array sub = a - b. Now the problem transcends to finding the sub array in this array sub (which has both positive and negative numbers) with zero sum. This can be done in O(n) time.

- Mitra November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice!

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

@mitar This equals to a subset sum problem, and can not be done in O(n). But the conversion is brilliant!

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

@Anonymous - Yes you can solve the problem of finding the first sub array that has sum of zero in O(n). I don't want to spoil the fun by spelling it for you. Hint - sum and hash set

- Mitra November 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

"finding the sub array in this array sub (which has both positive and negative numbers) with zero sum. This can be done in O(n) time."

is there a way 2 do dis in O(n) other than using hashing/mapping??

- @mitra November 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If I knew it, I would've been on the other side of table during interviews. :P

- Mitra November 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Mitra: Are you sure if you could solve subset sum problem in O(n) using extra space? If yes, can you please provide the link? I could find only the solution in O(n*sumof all integers in the array) !!

- rya November 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Guys how about this aproach

A = {0,0,0,1,1,0,1}

B = {0,1,0,1,0,0,0}

sum_a = {0,0,0,1,2,2,3} // i.e a[i] = a[i] + a[i - 1]
sum_b = {0,1,1,2,2,2,2}

now begin from the back of sum_a and sum_b 

if(sum_a[a_size] == sum_b[b_size])
{
   while(sum_a[a_size] == sum_b[b_size]){
    a_size --;
    b_size --;
   }
   return a_size; // from this index to 0 is the sequence we are looking for 
}
if(sum_a[a_size] > sum_b[b_size])
{
      b_size--;
}
if(sum_a[a_size] < sum_b[b_size])
{
        a_size--;
}
if( a_size == 0 || b_size == 0  )
return -1 ; // no such sequence exists

//I hope i am able explain logic clearly here , if any doubts let me know

- sachin323 November 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice sol bro...Excellente!!!

- Newbie January 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are searching the values always from 0-6... there could be sub array from 3-4 also... like below example.

int A[] = {0,0,0,1,1,0,0};
int B[] = {0,1,0,1,1,0,0};

Here the answer should be 3,4 / 2,6

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

Nice solution...But, don't you feel any need for an outer loop ??

- raj November 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bt its nt alws necessary dat one of d index is 0..

- @sachin323 November 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another solution ....

make an array c such that c[i]=b[i]-a[i]
Now the problem reduces to finding the maximum subarray such that sum of that subarray is zero.
This problem has been discussed before.
And is applicable for any numbers not only for 1 and 0's

- nitin November 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another solution ....

make an array c such that c[i]=b[i]-a[i]
Now the problem reduces to finding the maximum subarray such that sum of that subarray is zero.
This problem has been discussed before.
And is applicable for any numbers not only for 1 and 0's

- nitin November 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@nitin: "finding the maximum subarray such that sum of that subarray is zero" can u please provide the link?

- rya November 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@rya

question id is 4682948

- nitin November 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do it like this:
firstly, for each position in array a and b, record how many 1s before it.
1 0 1 0 1 0 => 0 1 1 2 2 3 3
0 1 1 1 0 1 => 0 0 1 2 3 3 4
then subtract the r_a(top one) from the r_b(bot one)
0 -1 0 0 -1 0 1
then your problem become to find the longest interval between two identical numbers. for example, here, it is 0: the first 0 appears at position 0 and the last 0 appears at 5. the interval between 0s is 5. no other numbers can make a longer interval than this.
to solve this problem: build two array s and q with size 2*n, n is the size of a or b;
for (int i = 0; i <= n; ++i)
{

s[diff[i]+n] = i;

}
so finally s records the first appearance position of each value (from -n to n);
then do it in an opposite way, ie i from n to 0
q records the last appearance of a value.
q[i] - s[i] is the longest interval of each value;
choose the largest one, output q[i] and s[i]-1;
done

- fentoyal December 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

time complexity 0(2n), space O(2n);

- fentoyal December 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it can be done in O(n) as follows.

take the bitwise and of both arrays A and B and store it in an array called Res[].The indices of first and last set bits of array Res[] will be the desired interval.


Please let me know in case if i am missing some thing.
Thanks.

- Sanjiv December 08, 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