Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
3
of 3 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
Comment hidden because of low score. Click to expand.
1
of 1 vote

In first example why it returned false when it had 3 qs or even a flush of 3 AKQ

- Anonymous August 08, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main() {
	bool pack[4][13] = {{0,0}};
	bool play=false;
	int pack_type=0,number = 0,cnt=0;
	
	cout<<"\nEnter pack_type (1-spade, 2-diamond, 3-heart, 4-club (0 for break):"; cin>>pack_type;
	cout<<"Enter number (Ace-1, 2-2,3-3..., J-11, Q-12, K-13 (0 for break):"; cin>> number;
	
	while (pack_type !=0 && number !=0) {
		if(pack_type <1 || pack_type>4 || number<1 ||number >13) 
			return -1;
		if (pack[pack_type-1][number-1]==true) {
			cout<<"Dup entry"; 
			return -1;
		}
		pack[pack_type-1][number-1]=true;
		cnt++;
		cout<<"\nEnter pack_type (1-spade, 2-diamond, 3-heart, 4-club (0 for break):"; cin>>pack_type;
		cout<<"Enter number (Ace-1, 2-2,3-3..., J-11, Q-12, K-13 (0 for break):"; cin>> number;
	}
	
	if(cnt<3) cout<<"Can't play!!";
	
	for(int i=0;i<4;i++) {
		for (int j=0;j<11;j++) {
			if (pack[i][j] && pack[i][j+1] && pack[i][j+2]) {
				cout<<"Trace1: I can continue to play!!";
				printf("Combo :\n %d->%d \n %d->%d \n%d->%d \n",i+1,j+1,i+1,j+2,i+1,j+3);
				play=true;
				break;
			}
		}
		if(play) {
			return 0;
		}
	}
	
	for(int i=0;i<2;i++) {
		for (int j=0;j<13;j++) {
			if(pack[i][j] &&pack[i+1][j] &&pack[i+2][j]) {
					cout<<"Trace 2:I can continue to play!!";
					printf("Combo :\n %d->%d \n %d->%d \n%d->%d \n",i+1,j+1,i+2,j+1,i+3,j+1);
					play=true;
					break;
			}
		}
		if(play) {
			return 0;
		}
	}
	for(int i=0;i<4;i++) {
		for (int j=0;j<11;j++) {
			cout<<pack[i][j];
		} 
		cout<<'\n';
	}
	cout<<"I cannot continue to play!!";
	return -1;
}

- alanturing06022014 August 02, 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