Amazon Interview SDE-1 (2024)

ssh3

shashi

Posted on July 24, 2024

Amazon Interview SDE-1 (2024)

the question asked was below & my approach and thought process

Image description

public class Amazon {

    /*
     * There is a town of N people with k sheriffs. Each person trust some people and mistrust others.
     * The town sheriffs would only trust other sheriffs but would be trusted by everyone else.
     *
     * Given the function:
     * trusts(Person source, Person target) which returns a boolean,
     *
     * Find all the town sheriffs.  ( police --> police) , allPeople --> allPolices
     */
/*

   1 .... N-k -.  .. r.. .. N

           n* (n-k) ---> 1 frist sheriff  ,  O (r-k)  -->  n ^2


           1 ,2 , 3

            sheriff -->  3,
            can't ve--> 1


           1,3 --> true
           2,3 --> false,


           1,2


           1->2 - true
           2->3 - false
           2->4

           */

    /* T : O(n*n) S : O(1)*/
    Person findSherif( Person[] people){


        for( int i=0; i<people.length; i++){

            boolean isSherif=true;

            for( int j=0 ; j< people.length; j++){

                if( i !=j && trusts(people[j], people[i] ) ){
                    isSherif=false;
                    break;
                }
            }

            if( isSherif) return people[i];
        }

        return null;


    }
}

Enter fullscreen mode Exit fullscreen mode

result :
not selected

đź’– đź’Ş đź™… đźš©
ssh3
shashi

Posted on July 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related