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

Store dictionary words in a trie for fast membership tests (much more memory efficient than a hash table).

For each letter, find the highest score possible for a word starting with that letter. Do this by first looking up that letter in the trie, then recursively looking up all possible follow-up letters. This will minimize the number of words which are considered which are not in the dictionary.

- nilkn March 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// scores Letter <-> score
map<char, int> scores;

// sort function
bool byScore(char c1, char c2) {
	if (scores[c1] > scores[c2]) {
		return true;
	} 
	if (scores[c1] < scores[c2]) {
		return false;
	}
	return c1 > c2;
}

// get the score of the dictionary word
int getWordScore(string word) {
	int score = 0;
	for (int i=0; i<(int)word.size(); i++) {
		score += scores[word[i]];
	}
	return score;
}
int getMaxScoreHelper(char *tiles, int len, set<string> voc, string word, string prefix) {
	int maxScore = -1;
	for (int i=0; i<len; i++) {
		string newWord = prefix;
		newWord += tiles[i];
		// check if the word exists in the dictionary
		if (voc.find(newWord) != voc.end()) {
			int score  = getWordScore(newWord);
			if (score > maxScore) {
				maxScore = score;
				word = newWord;
			}
		}
		char leftTiles[7];
		for (int j=0, k=0; j<len; j++) {
			if (i != j) {
				leftTiles[k] = tiles[j];
				k++;
			}
		}
		// check if the word can be "extended"
		score = getMaxScoreHelper(leftTiles, len-1, voc, word, newWord);
		if (score > maxScore) {
			maxScore = score;
		}
	}
	return maxScore;
}
// get the max word and score
// input: tiles, dictionary, output word
// return score, -1 if no word found
int getMaxScore(char tiles[], set<string> voc, string &word) {
	sort(tiles, tiles+7, byScore);	
	
	word = "";
	return getMaxScoreHelper(tiles, 7, voc, word, "");
}

- marinalepi March 06, 2015 | 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