Snap Interview Question: Given two words (beginWord an... | Glassdoor

Interview Question

Software Engineer Interview San Francisco, CA

Given two words (beginWord and endWord), and a dictionary's

word list, find the length of shortest transformation sequence from beginWord to endWord, such that: 1.Only one letter can be changed at a time 2.Each intermediate word must exist in the word list

1

Using BFS, Loop through 'a' to 'z' to replace each char of the neighbors word then check if it matches wordList.If so, add it into queue.count the level and return the level when you find a word which equals to the endWord.
here is my solution code:
public int ladderLength(String beginWord, String endWord, Set wordList) {
int level = 1;
q.offer(beginWord);
int cnt = q.size();
while(!q.isEmpty()){
String tmp = q.poll();
cnt--;
char[] cArr = tmp.toCharArray();
for(int i = 0; i &lt; cArr.length; i++){
for(char c = 'a'; c &lt;= 'z'; c++){
cArr[i] = c;
String str = new String(cArr);
if(str.equals(endWord)){
return level + 1;
}
if(wordList.contains(str)){
wordList.remove(str);
q.offer(str);
}
}
cArr[i] = tmp.charAt(i);
}
if(cnt == 0){
cnt = q.size();
level++;
}
}
return 0;
}

Interview Candidate on Jun 8, 2016
0

Assumes &quot;beginWord&quot; and &quot;endWord&quot; are the same length &quot;wordLen&quot;. Finds all possible combinations of words that are 1,2,...,wordLen away for both beginWord and endWord and calls them &quot;comb1&quot; and &quot;comb2&quot; respectively (ex: if begin word is &quot;now&quot;, Combo1 is made of 3 lists each consisting of words that are 1,2, and 3 changes respectively away from now like [[not, new, tow, cow, etc....], [nut, nap, bot, cot, etc... ], [lab, dad, rid, cat, etc... ] ]). Goes through both the combo sets and finds the intersection with the smallest number of transformations then returns the number of transformations.

main method -&gt; runs tests
getTransLen -&gt; compares array of arrays comb1 and comb2 to find smallest intersection
getTransformations -&gt; returns an array of arrays of words in dict that are 1,2,...,wordLen different from the word given
(ex: for &quot;now&quot;, [ [not, new, tow, cow, etc....], [nut, nap, bot, cot, etc... ], [lab, dad, rid, cat, etc... ] ])

https://raw.githubusercontent.com/dwyl/english-words/master/words.txt

==========================================

import java.util.*;
import java.lang.*;
import java.io.*;

public class SnapchatQ1 {

public static void main(String args[]) {
ArrayList dictList = new ArrayList();
try {
new FileInputStream(&quot;words.txt&quot;)));
try {
String line;
while ((line = br.readLine()) != null) {
}
} finally {
br.close();
}
} catch (Exception e) {

}

Set dictSet = new HashSet(dictList);
int answer1 = getTransLen(&quot;not&quot;, &quot;new&quot;, dictSet);
int answer2 = getTransLen(&quot;cold&quot;, &quot;tell&quot;, dictSet);
int answer3 = getTransLen(&quot;smile&quot;, &quot;speak&quot;, dictSet);
}

public static int getTransLen(String beginWord, String endWord, Set wordList) {
ArrayList comb1 = getTransformations(beginWord, wordList);
ArrayList comb2 = getTransformations(endWord, wordList);
int smallNum = -1;
for (int i = comb2.size() - 1; i &gt;= 0; i--) {
for (int j = 0; j ) comb2.get(i)) {
if (((ArrayList)comb1.get(j)).contains(currWord)) {
int newNum = i + j + 1;
if (newNum wordList) {
int wordLen = word.length();
ArrayList toReturn = new ArrayList();
for (int i = 0; i ());
}
for (String compWord : wordList) {
if (compWord.length() == wordLen) {
int diffNum = 0;
for (int i = 0; i ) toReturn.get(diffNum)).add(compWord);
}
}