Google Interview Question for Java Developers


Country: United States




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

Multiple solutions exist (based on the dictionary used), e.g.:
[fever, queue, zoo, mat, speaker, person, backgammon, house, wax, jury, diligent]

public static void fillInWords(String[] words) {
    Set<String> dict = generateDict();
    Set<Character> usedLetters = new HashSet<>();

    if (fillInWords(words, 0, "", dict, usedLetters)) {
        System.out.println(Arrays.toString(words));
    } else {
        System.out.println("No solution exists.");
    }
}

private static boolean fillInWords(String[] words, int wordIdx, String candidate,
                                   Set<String> dict, Set<Character> usedLetters) {
    // Final word completed
    if (wordIdx == words.length) return true;

    // Construct word
    StringBuilder sb = new StringBuilder(candidate);
    for (int i = sb.length(); i < words[wordIdx].length(); i++) {
        char currChar = words[wordIdx].charAt(i);

        // Fill in a blank and recurse or simply add current char to StringBuilder
        if (currChar == '*') {
            for (char c = 'a'; c <= 'z'; c++) {
                if (!usedLetters.contains(c)) {
                    sb.append(c);
                    usedLetters.add(c);

                    if (fillInWords(words, wordIdx, sb.toString(), dict, usedLetters)) {
                        return true;
                    }

                    // Backtrack
                    sb.setLength(sb.length() - 1);
                    usedLetters.remove(c);
                }
            }

            // No letter fits
            return false;
        } else {
            sb.append(currChar);
        }
    }
    candidate = sb.toString();

    // Test completed word
    if (candidate.length() == words[wordIdx].length() && dict.contains(candidate)) {
        if (fillInWords(words, wordIdx + 1, "", dict, usedLetters)) {
            words[wordIdx] = candidate;
            return true;
        }
    }
    return false;
}

- jgriesser February 03, 2018 | 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