Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

//input a 4X13 matrix with 4 suits and 13 ranks of cards. set cards[suit][rank] to 1 if this card in hand.
    public static boolean handClear(int[][] cards, int hand) {

        if(hand == 0) return true;

        for(int rank = 12; rank >= 0; rank--) {

            for(int suit = 0; suit < 4; suit++) {

                if(cards[suit][rank] == 1) { //if cards[suit][rank] in hand

                    cards[suit][rank] = 0; hand--;

                    int smallerRank = rank == 0 ? 12: rank - 1; // look for straight flush that end with this card
                                                                // watch for Ace as a special case that ***QKA and A23*** both valid
                    if(cards[suit][smallerRank] == 1) {

                        cards[suit][smallerRank] = 0; hand--;

                        int r = smallerRank - 1;

                        for(; r >= 0 && cards[suit][r] == 1; r--) {   //try playing the straight flush found

                            cards[suit][r] = 0; hand--;

                            if(handClear(cards, hand)) return true;

                        }

                        r++;

                        for(; r <= smallerRank; r++) {  //backtrack if play did not work

                            cards[suit][r] = 1; hand++;

                        }

                    }
                    //look for 3/4 of a kind for cards[suit][rand]
                    int n = cards[0][rank] + cards[1][rank] + cards[2][rank] + cards[3][rank];

                    if(n == 3 || n == 2) {

                        int tmp1 = cards[(suit + 1) % 4][rank],
                            tmp2 = cards[(suit + 2) % 4][rank],
                            tmp3 = cards[(suit + 3) % 4][rank];

                        cards[(suit + 1) % 4][rank] = 0; //try playing the 3/4 of a kind
                        cards[(suit + 2) % 4][rank] = 0;
                        cards[(suit + 3) % 4][rank] = 0;
                        hand -= n;

                        if(handClear(cards, hand)) return true;

                        cards[(suit + 1) % 4][rank] = tmp1;   //backtrack if play did not work
                        cards[(suit + 2) % 4][rank] = tmp2;
                        cards[(suit + 3) % 4][rank] = tmp3;
                        hand += n;

                    }

                    cards[suit][rank] = 1; hand++;
                }
            }
        }
        return false;

    }

Looking for interview experience sharing and coaching?

Visit aonecode.com for private lessons by FB, Google and Uber senior engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from Google, Facebook, Amazon, LinkedIn and other top tier companies after weeks of training.

- acoding167 July 12, 2019 | 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