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 votes

Another option is to populate the trie with all combinations of the allowed string, with an additional wildcard option (*) in the trie
eg: 'apple' will be added as 6 words - 'apple', '*pple', 'a*ple', 'ap*le', 'app*e', 'appl*'
when searching for an input word, search for exact match first
if not found try with the wildcard option at one position at a time
At each position, there are only two possible options to check the trie match - the exact char or the wildcard char, so search complexity is proportional to 2n operations (where n is the length of the search string)

- CodeMaster December 27, 2019 | Flag
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.
0
of 0 vote

The algorithm is as follows.
1. Construct a prefix trie with the given words.
2. Starting from the empty prefix of the word to the complete word, iteratively do the following. Assume that the letter following the prefix is the typo and should be skipped, and check if any subtries starting from that prefix are contain the suffix of the word. For example, if the input word is "posqfix", then check if in the subtrie starting at "", for all the children of "", if "osqfix" is a valid suffix. Next, assume that the valid prefix is "p", and check if in the subtrie starting at "p", for all the children of "p", if "sqfix" is a valid suffix. And so on until the valid prefix "posqfi", and the suffix is ""

# %pip install english-words

from english_words import english_words_set
from typing import Dict, Set, List, Optional

class TrieNode:
  """
  Represents a node in a Prefix Trie.
  """
  def __init__(self, letter: str, ends_word: bool):
    self.letter: str = letter  # The letter corresponding to the node
    # If True, then the sequence of letters leading up to this node represent a
    # valid word.
    self.ends_word: bool = ends_word 
    self.children: Dict[str, "TrieNode"] = {}

  def add_word(self, word: str) -> "TrieNode":
    """
    Adds a word to Trie starting at this TrieNode. 
    """
    ends_word = True if len(word) == 1 else False
    first_letter = word[0]
    if first_letter not in self.children:
      self.children[first_letter] = TrieNode(first_letter, ends_word)
    if ends_word:
        self.children[first_letter].ends_word = True
        return self.children[first_letter]
    # Recursively, add the suffix following the first letter.
    self.children[first_letter].add_word(word[1:])
    return self.children[first_letter]

  def add_words(self, words: Set[str]) -> None:
    for word in words:
      self.add_word(word)

  def contains(self, word: str) -> bool:
    """
    Returns True if the Trie starting at this TriNode contains the word.
    """
    if len(word) == 0:
      if self.ends_word:
        return True
      else:
        return False
    letter = word.lower()[0]
    if letter not in self.children:
      return False
    if len(word) == 1:
      return self.children[letter].ends_word
    # word is longer than one letter and the letter is in one of the children
    return self.children[letter].contains(word[1:])

  def is_prefix(self, word: str) -> bool:
    """
    Returns True if the word is a prefix of the words in the Trie starting
    at this TrieNode
    """
    if len(word) == 0:
      return True
    letter = word.lower()[0]
    if letter not in self.children:
      return False
    # if len(word) == 1:
    #  # last letter exists in the Trie. So is a prefix
    #  return True
    # word is longer than one letter and the letter is in one of the children
    # Recursively, check if the suffix is a valid prefix of the subtrie
    return self.children[letter].is_prefix(word[1:])

  def get_prefix(self, word: str) -> List["TrieNode"]:
    """
    If the word is a valid prefix in the Trie starting at this TrieNode, then
    this function returns the list of TrieNodes corresponding to this word.
    Else, it returns list with just self
    """
    current_level = self.children
    current_node = None
    prefix = [self]
    if not self.is_prefix(word):
      return prefix
    for letter in word.lower():
      current_node = current_level[letter]
      current_level = current_node.children
      prefix.append(current_node)
    return prefix

  # Unused. Not needed.
  def get_longest_prefix(self, word: str) -> List["TrieNode"]:
    """
    Returns the list of TrieNodes that constitute longest prefix of the word in
    the Trie starting at this TrieNode
    """
    current_level = self.children
    current_node = None
    prefix = [self]
    for letter in word.lower():
      if letter not in current_level:
        break
      current_node = current_level[letter]
      current_level = current_node.children
      prefix.append(current_node)
    return prefix

  def __repr__(self):
    return f"[{self.letter}][{self.ends_word}][{set(self.children.keys())}]"

trie = TrieNode('', False)
trie.add_words(english_words_set)

def get_one_letter_off_word(trie: TrieNode, word: str) -> Optional[str]:
  if trie.contains(word):
    return None
  # Iterate backward from the empty prefix to the entire word minus the last letter.
  for index in range(len(word)):
    index_of_candidate_skip_letter = index
    prefix_word = word[:index_of_candidate_skip_letter]
    if not trie.is_prefix(prefix_word):
      # Not a valid prefix. So no subtrie to search.
      continue
    index_of_suffix_start = index_of_candidate_skip_letter + 1
    suffix_word = word[index_of_suffix_start:]
    current_trie_node = trie.get_prefix(prefix_word)[-1]
    # print(f"Current trie node is {current_trie_node} at prefix {prefix_word}")
    for letter, child_node in current_trie_node.children.items():
      if word[index_of_candidate_skip_letter] == letter:
        # print(f"Ignoring child node {child_node}")
        # Dont consider subtries that match this letter.
        # are are looking for one letter off.
        continue
      if child_node.contains(suffix_word):
        # print(f"Found suffix {suffix_word} at {child_node}")
        return f"{prefix_word}{letter}{suffix_word}"
      # else:
        # print(f"Failed search for {suffix_word} at {child_node}")
  return None

assert get_one_letter_off_word(trie, "applx") == "apple"

trie.add_word('predate')
trie.add_word('predict')
assert trie.contains('predate')
assert trie.contains('predict')
assert get_one_letter_off_word(trie, 'predite') == "predate"

trie.add_word('ok')
assert get_one_letter_off_word(trie, 'qk') == 'ok'
assert get_one_letter_off_word(trie, 'aawdefcz') is None

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

import math

from typing import List
from collections import namedtuple
from pprint import pprint


class Point2D(namedtuple('Point2D', ['x', 'y'])):

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

    def __hash__(self):
        return hash(tuple([self.x, self.y]))


def euclidean_distance_2d(point1: Point2D,
                          point2: Point2D) -> float:
    return math.sqrt((point1.x - point2.x)**2 + (point1.y - point2.y)**2)


class DSU2DPoint:

    def __init__(self,
                 points: List[Point2D],
                 k: float = 2):
        """

        :param points:
        :param k:
        """
        self.points = points
        self.parents = {}
        self.make_set()
        self.threshold = k

    def make_set(self):
        for point in self.points:
            self.parents[point] = point

    def find_set(self,
                 point: Point2D):
        if point == self.parents[point]:
            return point
        self.parents[point] = self.find_set(self.parents[point])
        return self.parents[point]

    def union_sets(self,
                   point1: Point2D,
                   point2: Point2D):
        distance = euclidean_distance_2d(point1, point2)
        if distance <= self.threshold:
            a = self.find_set(point1)
            b = self.find_set(point2)
            if a != b :
                self.parents[b] = a

    def groups(self):
        return len(set(self.parents.values()))



def group_2D_points(points: List[Point2D],
                    k=2):

    dsu = DSU2DPoint(points, k)

    for point_1 in points:
        for point_2 in points:
            if dsu.parents[point_1] != dsu.parents[point_2]:
                dsu.union_sets(point_1, point_2)

    pprint(dsu.parents)

    return dsu.groups()


if __name__ == "__main__":
    xx = [1, 2, 4, 7, 0, 9]
    yy = [1, 0, 1, 4, 1, 10]

    points = []

    for x, y in zip(xx, yy):
        point_2d = Point2D(x=x, y=y)
        points.append(point_2d)

    count = group_2D_points(points, k=3)
    print("Total groups %s" % count)

- Anonymous December 24, 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