Google Interview Question for Software Engineers

Country: United States
Interview Type: In-Person

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

Looking for interview questions sharing and mentors?
Visit A++ Coding Bootcamp at (select english at the top right corner).

We provide ONE TO ONE courses that cover everything in an interview from the latest interview questions, coding, algorithms, system design to mock interviews. All classes are given by experienced engineers/interviewers from FB, Google and Uber. Help you close the gap between school and work.

Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.

Welcome to email us with any questions. Thanks for reading.


This question is actually asking for implementation of a TRIE that keeps track of the frequency of terms.

The search() method will return all the words with the input prefix which takes a lot of time. You may adjust it according to the interviewer's demand so that perhaps only the top 10 frequent keywords get returned.

import java.util.List;
import java.util.ArrayList;

class TrieNode {

    char letter;
    TrieNode previousLetter;
    TrieNode[] nextLetters;
    int frequency;
    boolean isEndOfWord;

    public TrieNode(char letter, TrieNode previousLetter) {
        this.letter = letter;
        this.previousLetter = previousLetter;


class Trie {

    private TrieNode root;

    class WordFrequency{        //keeps track of the frequency to rank the words
        String word;
        int frequency;

        WordFrequency(String w, int f) {
            word = w;
            frequency = f;

    public Trie(int sizeCharSet) {
        root = new TrieNode('&', null);
        root.nextLetters = new TrieNode[sizeCharSet]; //sort by freq in descending order

    public boolean insert(String word) {            //if the word is new return true, else false;
        TrieNode current = root;
        current.frequency++;                       //root.frequency stores how many words in total are in the Trie
        for(int i = 0; i < word.length(); i++) {

            int letter = word.charAt(i) - 'a';
            if(current.nextLetters[letter] == null) {               //initialize children
                current.nextLetters[letter] = new TrieNode(word.charAt(i), current);

            current = current.nextLetters[letter];
        if(current.isEndOfWord) {
            return false;
        else {
            return true;

    public List<WordFrequency> search(String prefix) {   //returns a list of words with the given prefix
        List<WordFrequency> autoCompletion = new ArrayList<>();

        TrieNode current = root;

        for(char c: prefix.toCharArray()) {
            if(current.nextLetters[c - 'a'] == null) {
                return autoCompletion;
            } else {
                current = current.nextLetters[c - 'a'];
        List<WordFrequency> surfix = new ArrayList<>();
        depthFirstSearch(current, surfix, new StringBuilder());

        for(WordFrequency wf: surfix) {
            wf.word = prefix + wf.word;

        //you may rank the autoCompletion by frequency according to the requirement in the follow-up question
        return autoCompletion;

    private void depthFirstSearch(TrieNode current, List<WordFrequency> surfix, StringBuilder str) {
        if(current == null) return;


        if(current.isEndOfWord && str.length() > 0) surfix.add(new WordFrequency(str.toString(), current.frequency));  //word found

        for(TrieNode nextLetter: current.nextLetters) {
            depthFirstSearch(nextLetter, surfix, str);
        str.deleteCharAt(str.length() - 1);


- aonecoding February 19, 2017 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


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


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