Coupon Dunia Interview Question for SDE1s


Country: India




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

The approach is to build a graph with vertices as words. An edge exists between two vertices if the two words differ by only one character. Once the graph is constructed, you need to a BFS to obtain the path/count the number of nodes between two words.

To construct the graph, we pass the dictionary as a Set<String>

public Map<String, List<String>> constructGraph(Set<String> set) {

	Map<String, List<String>> map = new HashMap<String, List<String>>();
	for (String str : set) {
		for (String match : set) {
			if (oneCharDiff(str, match)) {
				List<String> list = map.get(str);
				if (list == null) {
					list = new LinkedList<String>();
					map.put(str, list);
				}
				list.add(match);
			}
		}
	}
	return map;
}

In the above code, the function oneCharDiff returns true if there is exactly one character difference between two words.

The following code navigates to the path using the above function,

public static void path(String input, String output, Set<String> set) {

	Map<String, List<String>> map = constructGraph(set);
	Map<String, String> path = new HashMap<String, String>();
	Queue<Word> queue = new LinkedList<>();
	String parent = null;
	queue.add(new Word(input, parent));
	while (!queue.isEmpty()) {
		Word word = queue.remove();
		input = word.original;
		parent = word.parent;
		if (!path.containsKey(input)) {
			path.put(input, parent);
			if (input.equals(output)) {
				System.out.println(output);
				while (parent != null) {
					System.out.println(parent);
					parent = path.get(parent);
				}
				return;
			}
			List<String> list = map.get(input);
			if (list != null) {
				for (String child : list)
					queue.add(new Word(child, input));
			}
		}
	}
	System.out.println("No such path found");
}

Word is a simple class with 2 members - String original and String parent. This is used to print the order of the path followed.

Time complexity : O(n^2) when constructing the graph. Any suggestions to improve the solution are welcome. There is another way to handle this problem using the backtracking method but i'm sure the complexity would be exponential in that case.

- arsragavan March 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why mess with a graph at all? Just do the BFS on the words directly, checking all the possibilities against the dictionary. Sure, that is 26*3 possible words you have to try at each node, but it is O(n).

- tjcbs2 March 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

The answer is the number of letters in start word which are different from the letters in the finish word.
Java code:

public class StartFinish {
	private static String start = "cat";
	private static String finish = "hat";
	public static void main(String[] args) {
		System.out.println(findMinimumSteps(start, finish));
	}
	
	public static int findMinimumSteps(String start, String finish) {
		int j = 0;
		int cnt = 0;
		for(int i = 0; i < start.length(); i++) {
			if(start.charAt(i) != finish.charAt(j)) {
				cnt++;
			}
			j++;
		}
		return cnt;
	}

}

- Sourabh Das March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The problem in your approach is that.if I want to reach cat to dog it's not always a 3 step process.. What if this is the path followed

cat -> bat -> bot -> dot -> dog. So, it takes 4 steps but your answer will be 3.

- arsragavan March 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The minimum no of steps to reach cat to dog is three

cat -> dat -> dot -> dog

- varun.me15 March 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In question it is written in minimum steps

- sergio ramos March 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think there is no need of graph or tree. The question is asking minimum no of steps which is in worst case will be 3(three letter words: three bits to flip). Simple loop should work.

Suggestion and corrections welcomed :)

public class ChangeWord {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// cat -> dog : dat -> dot -> dog;
		int steps = findMinimumStepsToChange("cat", "dog");
		System.out.println(steps);
		// pic -> cat : pic -> cic -> cac -> cat
		int steps2 = findMinimumStepsToChange("pic", "cat");
		System.out.println(steps2);

	}
	
	
	public static int findMinimumStepsToChange(String word1, String word2){
		if("".equals(word1)||"".equals(word2))
			return -1;
		if(word1.equals(word2))return 0;
		
		int steps = 0;
		int charCount = 0;
		
		for(int i=0;i<word1.length();i++){
			
			if(word1.charAt(i) != word2.charAt(i)){
				
				steps++;
			}
		}
		
		return steps;
	}

}

- varun.me15 March 30, 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