Google Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

Preprocess the dictionary into a trie. On every iteration we will have letter z of the input word and set S comprising of pairs (x,r) where x indicates our non-matching character allowance, and r is a trie node. Initially we put (1,R) to S, where R is the trie root node. For every node (x,r) in S and every child node w of r we put (x,w) back to S if w corresponds to z; otherwise we put (x-1,w) provided that x>0. Terminate when an end-of-word child node is encountered meaning we found a match. Complexity: quadratic worst case. This is because on every iteration the increment of the size of S is bounded by constant L (the length of the alphabet), as entries with x==0 can only 'spawn' at most 1 new entry in place of itself and at every point there is only 1 entry with x==1 which can spawn at most L new entries (with x==0) giving us O(n*n*L) = O(n*n).

D = ['apple', 'apple', 'banana', 'orange']

def preprocess_dict():
    trie = {}
    for word in D:
        r = trie
        for z in word:
            r = r.setdefault(z, {})
        r[None] = []
    return trie

R = preprocess_dict()

def word_exists(word):
    s = [(1,R)]
    for z in word:
        next_s = []
        for (x,w) in s:
            if z in w:
                next_s += [(x,w[z])]
            elif x>0:
                next_s += [(x-1,r) for r in w.values()]
        s = next_s
    return any(None in w.keys() for (x,w) in s)

print (word_exists('applx'))

- adr October 02, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

An alternative O(n*n) solution would be to preprocess the dictionary into a hash map thus: for every dictionary word w of length q add q words to the hashmap which can be created by removing q-th letter from the word w. Example: for the word 'dog' we add 'og', 'dg', 'do' into the hashmap. Then for the input word we do a similar thing: remove i-th letter and see if the resultant word is in the hashmap. It's quadratic because it takes O(n) to hash the word and we need to do it n times.

D = ['apple', 'apple', 'banana', 'orange']

def preprocess_dict():
    hmap = set()
    for word in D:
        for i in range(len(word)):
            hmap.add(word[:i] + word[i+1:])
    return hmap

H = preprocess_dict()

def word_exists(word):
    for i in range(len(word)):
        if (word[:i] + word[i+1:]) in H:
            return True
    return False

print (word_exists('aPple'))

- adr October 02, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

for every character in the string
toggle each character by +1/-1
generate the new string
hash the new string with the dictionary and check if it is present

- Pooja October 02, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

string checknearest(string str,map<string,int>dict)
{

for(int i=0;i<str.length();i++)
{
string tmp=str;

tmp[i]++;
if(dict.find(tmp)!=dict.end())
return tmp;
tmp=str;
tmp[i]--;
if(dict.find(tmp)!=dict.end())
return tmp;

}

return "";
}

- k.pooja.222 October 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