Amazon Interview Question for SDE1s


Country: India
Interview Type: Written Test




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

This seems to be a permutation of the set cover problem. The followers can be seen as members of the set. In the standard set cover problem, we look for the smallest number of sets that covers all members. In this case, we look for a set that covers 'k' members, but that doesn't reduce the worst-time complexity, so any 'perfect and correct' answer will be NP-complete to my thinking, since you would have to try all combinations.

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

i also agree with you its NP-hard problem worst case complexity is exponential....

- Anonymous July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

Agree! However how can you show that releasing the constraint from asking a cover of the whole set to asking a cover of at least k elements still generate a NP-hard problem? (I can't)... For example for k=1 it is not (but for k=n it is) :)

- Chih.Chiu.19 July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@chih.chiu.19 ya u r correct not for all k...

- Anonymous July 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Chih.Chiu.19 and @Anonymus... Whether its NP-Hard or not, I can say that the time complexity is exponential in terms of k. Putting k=1 in this case seems "easy" just as in case of putting n=1 for polynomial-time algorithm in terms of n.
n=1 case is always "easy" for any algorithm, whether it be a poly-time or exponential time algorithm.

- EOF September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

On the face of it, appears to be Dynamic Programming.

Min_#_of_players_with_fan_count_sum(k) = Min( 1 + Min_#_of_players_with_fan_count_sum(k - fan_count_of_current_player),
Min_#_of_players_with_current_player_excluded_and_fan_count_sum(k))

Time complexity- polynomial in n.

- Murali Mohan July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

well i thought about it but the distinct followers thing puzzled me out.I don't think it may be applied here.Correct me if iam wrong.

- Sibendu Dey July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

By the way im confused whether by k we mean distinct followers or a follower following two players is counted as 2 in the context of k

- Sibendu Dey July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sibendu Dey

When the question says "You have to select minimum number of players so that the total followers must be equal to a given number 'K'.", why do you want to make assumptions about the distinctness of the followers?

The example given is valid in the sense that you can combine player 0 and player 2 to get answer. You can as well combine player 0 and player 1 to get the answer. The combination need not be unique.

And when it comes to the specification asking for 'minimum' number of users, it is clearly an optimization problem and it is clear that you can use DP to solve the problem. Remember DP also gives "an" optimized solution to a problem and not "the" optimized solution as an optimization can have more than one solution.

- Murali Mohan July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think we need a comment for @P3A about this issue.The example which he gave clearly brings out a debate on the "distinct" issue.He might have said in the example:The solution is player 1 and player 2 or Player 1 and player 3,but he did'nt.That clearly brings some doubt.

- Sibendu Dey July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sibendu

Yeah, you are right! But I am doubtful if P3A himself had clarified this with the interviewer :-)

- Murali Mohan July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ayahuasca:Yes ofcourse me too

- Sibendu Dey July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I also think this is the dynamic programming problem.

/**
* we define the following function f(int []a, int k, Char [] f)
* where a is the set of players used
* k is the required # of followers
* f is list of current followers
* output is the # player used.
* f(a, k, f)=min_i {f(a U i, k, merge(f, arr[i]))}
*/

- zhangtemplar August 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Erasmus ... If followers needn't be distinct then why would one give the input format in the form of binary strings. The interviewer could just give the count of followers for each player.
Odds are great that followers must be distinct!

- EOF September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a) answer is always <= N. b) answer is the total no. of followers for the combination of players.

the concept is about nCr.
start the loop r value from 1 and increase it by 1 n.
for each combination in nCr do the operation as if there atleast one 1 exist then count it,
ex in 2's combination 111100
100010
----------------
111110 (count no of 1s)
----------------
in 3's combination
111100
000100
100010
---------------
111110 (count no of 1s)
if no of 1's in any combination is equals to k, then break the loop and return r value.

------ hope this will help u to find solution

- Anonymous July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please help me in understanding the problem.
Does being 1 at Jth place really matter? I think only thing which matters is that how many 1s are present in any number of lines?

- Aks July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the value of K can take any value ,then the place of 1's doesn't matter,we can apply DP to solve it.But if the value of K need to be less than N,then it matters.

- Sibendu Dey July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone say that the value of K need to be necessarily lesser than N or it can take any value ?

- Sibendu Dey July 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first we have to find out the number of follower for the first player. by counting no. of 1s. then we will add the player in player list if he has some distinct follwers than first one. and we will keep adding the players until we will sum followers up to k.

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

@Anonymous:Counter example:How about this?
3 6 5
111100
000011
111110
According to your solution Player 1 and player 2 will be the answer
But the solution player 3 gives us an minimum solution.

- Sibendu Dey July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

its a combinatorics problem first check for one player who has k number of followers if not check for 2 players with k=P(ni)+p(nj)-p(ni ^nj) where ^ is intersection. similarly repeat for 3 variables and so on...

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

Algorithms with exponential time complexity are not useful, unless in a very narrow requirement sense.

- Anonymous July 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

actually P class is very small when compared to NP class and this problem looks like NP class problem.....

- Anonymous July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

P=NP is an open million $ question and here we have ppl talking about 'very small'

- Anonymous July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

here small is class of problems that belongs to polynomial time are small in number when compared to non polynomial problems....

- Anonymous July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous:

NP is not "non-polynomial". It is non-deterministic polynomial (i.e. polynomial time, on a non-deterministic Turing Machine).

It is an open question whether NP is in fact, same as P. Only if P is not NP, will your statements be correct.

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

with somewhat analysis you an appreciate its similarity with 'printing the permutations of a string'. Here also for each player decisions can be made wrt the previous player's followers and for only current player. local hash arrys can be maintained and referenced for subsequent players. Below is the working code tested in C:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

int min(int,int);
int func(int i,int j,int k,int n,int **matrix,int *hash,int *count);
int main()
{
    int i,j,k,**matrix,temp,temp1,*hash,count;
    printf("enter the values for number of(players,followers and desired followers i,j,k)\n");
    scanf("%d%d%d",&i,&j,&k);
    matrix=(int **)malloc(i*sizeof(int *));
    for(temp=0;temp<i;temp++)
    matrix[temp]=(int *)malloc(j*sizeof(int));
    printf("enter the values\n");
    for(temp=0;temp<i;temp++)
    {
         for(temp1=0;temp1<j;temp1++)
             scanf("%d",&matrix[temp][temp1]);
         printf("\n");
    }
    printf("values added are\n");
    hash=(int *)malloc(j*sizeof(int));
    for(temp=0;temp<i;temp++)
    {
         for(temp1=0;temp1<j;temp1++)
         {
             printf("%d ",matrix[temp][temp1]);
             hash[temp1]=0;
         }
         printf("\n");
    }
    
    
    count=0;
    printf("minimum number of players required are:%d",func(0,j,k,i,matrix,hash,&count));
    getch();
    return 1;
    
}

int func(int i,int j,int k,int n,int **matrix,int *hash,int *count)
{
    if(i>=n)
    return -1;
    int *hash1,tCount,t;
    hash1=(int *)malloc(j*sizeof(int));
    for(t=0;t<j;t++)
    {
        if(matrix[i][t]==1)
        {
            hash1[t]=1;
            tCount++;
            if(hash[t]!=1)
            {
                 hash[t]=1;
                 *count++;     
            }
            if(tCount==k||*count==k)
            return 1;
        }
        else
        {
            hash1[t]=0;
        }
    }
    return min(func(i+1,j,k,n,matrix,hash,count),1+func(i+1,j,k,n,matrix,hash1,&tCount));
}

int min(int x,int y)
{
    return (x<y&&x!=-1)?x:y;
}

output:
=======
enter the values for number of(players,followers and desired followers i,j,k)
3 6 5
enter the values
1 1 1 1 0 0

0 0 0 0 1 0

1 1 1 1 1 0

values added are
1 1 1 1 0 0
0 0 0 0 1 0
1 1 1 1 1 0
minimum number of players required are:1

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

String str = "111100";
String str2 = "000100";
String str3 = "000010";
int kk=0;
for(int i=str.length()-1;i>=0;i--){

if(str.charAt(i)=='1'){
a = a | (1<<kk);
}

kk=kk+1;
System.out.println(a);
}
kk=0;
int b=0;
for(int i=str2.length()-1;i>=0;i--){

if(str2.charAt(i)=='1'){
b = b | (1<<kk);
}

kk=kk+1;
System.out.println(b);
}
kk=0;
int c=0;
for(int i=str3.length()-1;i>=0;i--){

if(str3.charAt(i)=='1'){
c = c | (1<<kk);
}

kk=kk+1;
System.out.println(c);
}
System.out.println(c);
int aa[] = {a,b,c};
int N=3;
int M=6;
int K=5;
for(int i=1;i<(1<<N);i++){
int r =0;
for(int j=0;j<N;j++){
if((i & (1<<j)) !=0){
r = r | aa[j];
}
}
if(K==Integer.bitCount(r)){
System.out.println("found");
break;
}
}

- algolearner August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR EVERY LINE WITH N-1 REMAINING LINE GIVES U DISTINCT NO OF FOLLOWERS.
then use DP

- @#$% September 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

How is it 0th and 2nd. That adds up to 6. 0th and 1st adds up to 5. Am I understanding the question wrong?

- ambu.sreedharan July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think by total number of followers=k,he means no. of distinct followers...(bitwise OR of 0th and 2nd)

- Amit July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

however if correct answer is 0th and 1st as you said,then the given prob boils down to the subset sum prob...correct me if i am wrong

- Amit July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

sreedhar the problem and solution is correct.

one or more players can have the same followers.

the solution should be the minimum no.of players whose followers count is equal to k. understood??

- vignesh July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

got it. Sounds like there are M number of individuals who follow. And each of them can follow more than one. We need K number of individual followers.

- ambu.sreedharan July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Amit

Both 0,1 and 0,2 are valid answers. You can provide as an answer any one of many optimal solutions to a problem. Remember, for optimization problems, an optimal solution need not be unique, there can be multiple

- Murali Mohan July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

our solution revolves around k.

in our example k=5;

no players have total followers 5. so we go for combination of 2 players.

how many followers does player0 have? 4
how many followers does player2 have? 1
both players have independent followers. and count is 5. therefore minimum 2 players are enough to meet k condition.
if no 2-player combination met k.(tot followers value)
we will go for 3-players combination. hope u get the problem.

- vignesh July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the time complexity of your solution is exponential

- Amit July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if K=M, and K means different followers, then the problem is exactly the Set Cover problem which is NP-Complete.

- jicheninsjtu July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

maintain an array containing number of 1's from each of these players (3 in this case)
then it is simply, sum of sub array problem
(www dot to be put here geeksforgeeks.org/find-subarray-with-given-sum)

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

Just a change required,you dont need to find the subarray,you need to find a minimum length subsequence that matches the sum.

- Sibendu Dey July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Hi

1)sorting in desc order of the array of 0 and 1
2) doing xor and counting the number of ones in the result till we get desired count

correct me if i am wrong pls

- logicfinder July 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@logicfinder:Can u please give an example?

- Sibendu Dey July 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

take the same example..

sorting the array we get ,
111100
000100
000010

doing xor with first and second we get
111000- we get k value as 3

if we xor with first and third, we get
111110 - 5

hence the solution.

complexity is log N + N , N is no of players

- logicfinder July 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@logicfinder - this doesnt work. what if k = 6 and the array looks like:
111100
000110
000010
000001, from your logic the minimum number required will be 6 (by xor-ing 1st, 3rd & 4th). But the correct answer is 2 (1st and 2nd), which when xor-ed will return 111010 (only 4)

- hj July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

On the face of it it looks like a classic problem of dynamic programming, since there are a possible on 2 exp(n) number of cases to consider and we need to find the minimum no of players. But we can see that a greedy approach will work here...

1. Find the no of followers for each player(takes O(n) time)
2. Sort the followers in decreasing order(takesO(nlogn))
3. while K is less than followers in sorted array, add player into set, and decrease the no of followers and go further
4. If at any point, the number K is not possible, backtrack to the latest selection, remove from the selected set, and start from the value after that

Eg if followers are 11 9 7 5 3 2
k = 23
11, 9, 3 are minimum set
if followers are 7 6 3 2
k = 8
choose 7, now from 6, 3, 2 we cant make 8(7+1), hence backtrack remove 7, and now start from 6,
k = 8 and set is 6, 3, 2
minimum set is 6, 2
In the worst case and k is not possible, we choose n times and backtrack n times, leading to a O(n^2) time but in most cases, problem is solved in O(n) time.

- praveen July 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 6 vote

Its a 0-1 knapsack problem. We have to minimize number of people given a total number of followers.

- Nitin Gupta July 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Good one, Thumbs up! Though knapsack problem is a maximization problem, and the question here is about minimization, the principle used to solve knapsack problem can nonetheless be applied by negating all the numbers to find out maximum value of the result. The result can again be negated to get minimum value.

Another DP approach that comes to my mind should also work.

Min_#_of_players_with_fan_count_sum(k) = Min( 1 + Min_#_of_players_with_fan_count_sum(k - fan_count_of_current_player),
Min_#_of_players_with_current_player_excluded_and_fan_count_sum(k))

- Murali Mohan July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

Hold on... there is no such thing as "overlap" in knapsack problem right? But here overlap need to be considered.

- Chih.Chiu.19 July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

did u see here overlap need to be considered.

- blackfever July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@blackfever overlap needs to be considered otherwise player 0& 1 can also be a soln but then followers would be 4 only in given testcase

- bad coder July 21, 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