## Walmart Labs Interview Question for Software Developers

Team: Customer experience
Country: United States
Interview Type: In-Person

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

Can be modeled as a graph problem. Let the vertex be the set of all words that are given to us, including the start and end words.

Now, for each vertex, there exists an edge if a vertex can be converted to another vertex.
So, lets say we have "dog" and "cog". Since dog can be converted to cog by replacing one character, we ad an edge between them.

Once we are done constructing the graph, all we have to do is do a BFS between the start node and end node which also gives us the shortest path.

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

Yes that is the same approach I concluded.

It was very tricky for me to come into that conclusion and write the code.

Feel free to take a look at my algorithm bellow and let me know.

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

This was a very hard one for me to make it.

Actually I though of a recursive version with is n^n but then looking a bit closer I though that this is better to do using a graph algorithm which creating the graph would be n^2 and traversing it would be n using a BFS traversal.

``````public static bool PathExist(List<string> set, string start, string end)
{
if(string.IsNullOrEmpty(start) ||
string.IsNullOrEmpty(end) ||
start.Length != end.Length
{
throw new ArgumentException();
}

// Sanitize the set
var hs = new HashSet<string>();
foreach(string s in set)
{
if(s.Length == start.Length)
{
// Hashset ignores if is already there
}
}

// Building graph in n^2

var graph = Dictionary<string, HashSet<string>>();

foreach(var t1 in hs)
{
foreach(var t2 in hs)
{
if(IsOneLetterDiff(t1, t2))
{
if(!graph.ContainsKey(t1))
{
graph.Add(t1, new List<string>() { t2 });
}
else
{
}
}
}
}

// Now BFS traverse of the graph to find if a path exist
var q = Queue<string>();
var visited = new HashSet<string>();

q.Enqueue(start);

while(q.Count > 0)
{
string cur = q.Dequeue();

// Found a path
if(cur == end)
return true;
if(!visited.Contains(cur))
{

if(graph.ContainsKey(cur))
{
foreach(var child in graph[cur])
{
q.Enqueue(child);
}
}
}
}

return false;
}

// Assumes that their both non-null and with the same length
private static bool IsOneLetterDiff(string one, string two)
{
bool found = false;
for(int i = 0; i < one.Length; i++)
{
if(one[i] != two[i])
{
if(!found)
{
found == true;
}
else
{
return false;
}
}

return found;
}
}``````

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

A very simple Java logic

``````import java.util.ArrayList;
public class StartEndSetString {

private boolean getPaths(String start, String end, List<String> data) {
if(string_diff(start, end)==1){
return true;
}
Iterator<String> iterator = data.iterator();
while(iterator.hasNext()){
String values = iterator.next();
if(string_diff(values, start)==1){
iterator.remove();
if(getPaths(values, end, data)){
return true;
}
}
}
return false;
}

private int string_diff(String start, String end) {
int diff = 0;
for(int i=0;i<start.length();i++){
if(start.charAt(i)!=end.charAt(i)){
diff+=1;
}
}
return diff;
}

}``````

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

A very simple Java code

``````public class StartEndSetString {

private boolean getPaths(String start, String end, List<String> data) {
if(string_diff(start, end)==1){
return true;
}
Iterator<String> iterator = data.iterator();
while(iterator.hasNext()){
String values = iterator.next();
if(string_diff(values, start)==1){
iterator.remove();
if(getPaths(values, end, data)){
return true;
}
}
}
return false;
}

private int string_diff(String start, String end) {
int diff = 0;
for(int i=0;i<start.length();i++){
if(start.charAt(i)!=end.charAt(i)){
diff+=1;
}
}
return diff;
}``````

}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

It's more efficient to work backwards from the end string.

1. Eliminate words that are not the same length as the end word
2. Find words that differ by one char from end string, and remove these matched
words from search
3. Repeat (2) where end string becomes a matched word.

Linear time O(n^2). Space: O(1)

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

This actually would be n^n as every time you do step 2 you'll need to process n times found string n matches for those you'll need to process n matches and so and on.

So this proposed algorithm is actually n^n.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

Ruby impl. Running time O(nm). Space: O(1).

``````require 'set'

def exists_path?(start_str='',end_str='',str_set=Set.new)
return true if start_str == end_str || strings_diff_by_one?(start_str, end_str)
return false if start_str.length != end_str.length

original_end_str=end_str
path=[end_str]

while str_set.length > 0 && start_str != end_str &&
(found_str = find_str_with_diff_one(str_set,end_str))
str_set.delete(found_str)
end_str = found_str
path.insert(0,end_str)
end

path.length>=2 && path[0]==start_str && path[path.length-1]==original_end_str
end

def strings_diff_by_one?(string1,string2)
chars_changed=0

string1.chars.each_with_index do |character,index|
#puts "#{character} - #{str_value[index]}"
if character != string2[index]
chars_changed+=1
end
end

chars_changed==1
end

def find_str_with_diff_one(str_set, str_value)
return nil if str_set.empty? || str_value.to_s.length==0

found_str=nil

str_set.each do |str|
found_str=str if strings_diff_by_one?(str,str_value)
end

return found_str
end

start_str='cog'

puts "path exists?: #{exists_path?(start_str,end_str,Set.new(['bag', 'cag','cat', 'fag','con','rat','sat','fog']))}"``````

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

Actually this does not work as you assume that the first found is the actual path and there could be path that lead no-where.

Also this actually is a O(n^2 * m) algorithm even though is not working correctly.

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.

### 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.