Comments on: Programming Interview Questions 8: Transform Word /2011/10/17/programming-interview-questions-8-transform-word/?utm_source=rss&utm_medium=rss&utm_campaign=programming-interview-questions-8-transform-word Information Retrieval and Machine Learning Thu, 20 Jun 2013 16:29:00 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: AIR20 /2011/10/17/programming-interview-questions-8-transform-word/#comment-7581 AIR20 Mon, 20 May 2013 03:28:00 +0000 /?p=597#comment-7581 How did you generate the graph with blue circles in the post? It looks very nice. I’d like to make such a graph too.

]]>
By: Fatih /2011/10/17/programming-interview-questions-8-transform-word/#comment-7555 Fatih Sun, 03 Mar 2013 01:33:00 +0000 /?p=597#comment-7555 I think I should find a better way to share code here

]]>
By: Fatih /2011/10/17/programming-interview-questions-8-transform-word/#comment-7554 Fatih Sun, 03 Mar 2013 01:30:00 +0000 /?p=597#comment-7554 Here is the C# version

class Graph

{

Hashtable graph = new Hashtable();

public Graph(List englishDictionary)

{

foreach (string word in englishDictionary)

{

Object nodeObject = graph[word];

GraphNode node = null;

if (nodeObject == null)

{

node = new GraphNode(word);

graph.Add(word, node);

}

else

{

node = (GraphNode)nodeObject;

}

//check the added truncated ones

for (int i = 0; i < word.Length-1; i++)

{

string truncated = word.Substring(0, i) + word.Substring(i + 1);

if (englishDictionary.Contains(truncated))

{

Object nodeObject2 = graph[truncated];

GraphNode node2 = null;

if (nodeObject2 == null)

{

node2 = new GraphNode(truncated);

graph.Add(truncated, node2);

}

else

{

node2 = (GraphNode)nodeObject2;

}

node.connect(node2);

Console.WriteLine("[Truncated]Connected those two: " + word + ", " + truncated);

}

}

//check the addedOnes

string allEnglishCharacters = "abcdefghijklmnopqrstuvwxyz";

List allEnglishCharacterList = allEnglishCharacters.ToList();

foreach (char c in allEnglishCharacterList)

{

for (int i = 0; i <= word.Length; i++)

{

String addedString = word.Substring(0, i) + c + word.Substring(i);

if (englishDictionary.Contains(addedString))

{

Object nodeObject2 = graph[addedString];

GraphNode node2 = null;

if (nodeObject2 == null)

{

node2 = new GraphNode(addedString);

graph.Add(addedString, node2);

}

else

{

node2 = (GraphNode)nodeObject2;

}

node.connect(node2);

Console.WriteLine("[Added] Connected those two: " + word + ", " + addedString);

}

}

}

//Check the changes

foreach (char c in allEnglishCharacterList)

{

for (int i = 0; i < word.Length; i++)

{

String changedString = word.Substring(0, i) + c + word.Substring(i+1);

if (changedString.Equals(word))

{

continue;

}

if (englishDictionary.Contains(changedString))

{

Object nodeObject2 = graph[changedString];

GraphNode node2 = null;

if (nodeObject2 == null)

{

node2 = new GraphNode(changedString);

graph.Add(changedString, node2);

}

else

{

node2 = (GraphNode)nodeObject2;

}

node.connect(node2);

Console.WriteLine("[Changed] Connected those two: " + word + ", " + changedString);

}

}

}

}

}

public void showTheShortestPath(string source, string target)

{

GraphNode startNode = (GraphNode)graph[source];

if (startNode == null)

{

Console.WriteLine("No words found in the dictionary for input: " + source);

return;

}

foreach (GraphNode node in startNode.ConnectedNodes)

{

node.ParentInTraverse = startNode;

}

String result = traverseConnectedNodes(startNode, new List() { startNode }, target);

if (result == null)

{

Console.WriteLine(“Cannot reach to target”);

}

Console.WriteLine(result);

}

private String traverseConnectedNodes(GraphNode startNode, List visitedNodes, string target){

if (startNode.ConnectedNodes.Count == 0)

{

return null;

}

List children = new List();

foreach (GraphNode childrenNode in startNode.ConnectedNodes)

{

if (!visitedNodes.Contains(childrenNode))

{

visitedNodes.Add(childrenNode);

if (childrenNode.Value.Equals(target))

{

Console.WriteLine(“Found the target”);

return startNode.Value+”->”+ childrenNode.Value;

}

Console.WriteLine(“Adding childrenNode: ” + childrenNode.Value);

string result = traverseConnectedNodes(childrenNode, visitedNodes, target);

if (result != null)

{

return startNode.Value + “->” + result;

}

}

}

return null;

}

}

]]>
By: sonesh /2011/10/17/programming-interview-questions-8-transform-word/#comment-6503 sonesh Tue, 21 Aug 2012 07:39:54 +0000 /?p=597#comment-6503 thank you for your alternative, but i think, we also have make sure that all intermediate word should be their in dictionary which i think can’t be maintained while calculating Levenshtein distance ..!!!

]]>
By: Anonymous /2011/10/17/programming-interview-questions-8-transform-word/#comment-6302 Anonymous Sun, 12 Aug 2012 12:54:14 +0000 /?p=597#comment-6302 I think this can be done in a better manner using the Levenshtein distance approach, () and then backtracking the two dimensional array along the optimal path to compute the intermediate words.

]]>
By: Arden /2011/10/17/programming-interview-questions-8-transform-word/#comment-1701 Arden Mon, 30 Jan 2012 23:37:50 +0000 /?p=597#comment-1701 Yes you’re right, it’s a nice optimization. Thanks a lot for the comment.

]]>
By: Sachin /2011/10/17/programming-interview-questions-8-transform-word/#comment-1694 Sachin Mon, 30 Jan 2012 04:17:35 +0000 /?p=597#comment-1694 You check whether we reached target word after popping out the word from queue. But if you do the check before pushing it to the stack, then we can avoid unnecessary pushes into the queue.

Suppose if we have a level in the graph which has 100s of elements and the first element is the word that we are looking for, then we do not have to push the 100 elements.
[sourcecode language="python"]

for word in transforms:
if word == goal:
return currentPath[:]+[word]
if word not in currentPath:
#avoid loops
paths.append(currentPath[:]+[word])


[/sourcecode]

]]>
By: ANONYMOUS /2011/10/17/programming-interview-questions-8-transform-word/#comment-1357 ANONYMOUS Thu, 12 Jan 2012 05:19:05 +0000 /?p=597#comment-1357 please take a look at my implementation, basically I will check each possible intermediate word in dictionary.

here is my code :

]]>
By: Arden /2011/10/17/programming-interview-questions-8-transform-word/#comment-1339 Arden Wed, 11 Jan 2012 09:58:40 +0000 /?p=597#comment-1339 Classic edit distance solution would just give you a path, not necessarily the shortest path. Also precomputing the graph would be beneficial if we were asked many transformations. Otherwise we will exhaustively try all edits, most of which are not valid english words.

]]>
By: ANONYMOUS /2011/10/17/programming-interview-questions-8-transform-word/#comment-1330 ANONYMOUS Wed, 11 Jan 2012 00:17:32 +0000 /?p=597#comment-1330 this is just like the classic editing distance problem. build dictionary graph is unnecessary.

]]>