Interview Question for Developer Program Engineers






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

Hi,
I am not sure,but we can use BFS traversal to find the min no of steps.Where your source node would be HEAD and destination node would be TAIL,and at each level if a word doesn't belong to the dictionary we will remove it from the queue

- Anonymous September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sounds like it could be solved by generating a graph from the words in the dictionary where the source word is the root, the target word is one of the leaf nodes and the nodes in between are words of the same length, where there is an edge between word w1 and word w2 if there is exactly one char different between w1 and w2. Then just do dijkstra /shortest path on that graph.

- Anonymous September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we already have dictionary implemented and saved with us, it should be in m-way Tries and HEAD should be one of the words. We just have to walk up the trie finding 4 letter words in the trie (since dictionary is implemented in trie so all words are valid words). so it could be like head->heal->heel->h#el no valid word like this then step back->heal->hell->hall->tall (target letter on 1st place reached)->tail {answer}
comments expected..

- Roxanne September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isValid(String word)
  {
    Set<String> dict = new HashSet<String>();
    dict.add("cat");
    dict.add("car");
    dict.add("tar");
    dict.add("tor");
    dict.add("toy");
    dict.add("toz");
    if (dict.contains(word))
      return true;
    return false;
  }

  public static void main(String[] args)
  {
    System.out.println(minTransforms(Arrays.asList(0, 1, 2), "cat", 0, "toz",new HashSet<String>()));
  }

  public static int minTransforms(List<Integer> indxes, String word, int len, String dest,Set<String> visited)
  {
    if (word.equals(dest))
      return len;
    int lenT = Integer.MAX_VALUE;
    for (int i=0;i<word.length();i++)
    {
      if(word.charAt(i)==dest.charAt(i))continue;
      List<String> words = generateWords(i, word);
      List<Integer> ind = new ArrayList<>(indxes);
      for (String word_new: words)
      {
        if (!word_new.equals(word) && isValid(word_new) && !visited.contains(word_new))
        {
          //ind.remove(indx);
          visited.add(word_new);
          lenT = Math.min(minTransforms(ind, word_new, len + 1, dest,visited), lenT);
          visited.remove(word_new);
        }
      }
    }
    return lenT;
  }

  public static List<String> generateWords(int indx, String word)
  {
    List<String> words = new ArrayList<String>();
    char chr[] = word.toCharArray();
    for (char a = 'a'; a <= 'z'; a++)
    {
      chr[indx] = a;
      String str = new String(chr);
      words.add(str);
    }
    return words;
  }

- Koustav Chattterjee June 07, 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