Interview Question


Country: India
Interview Type: In-Person




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

Pseudo code:
// Form DAG of Char, smaller char have bigger chars as its children.
// Something like
// a->b
//  | |->|
//  |->->c (a points to b, c; b points to c, etc)
formDAG(Array<String> dict, Graph<Char> graph) {
	sort(dict); // Sort the dictionary
	Array<Char> chars = new Array<Char>();
	Array<String> recurseDict = new Array<String>();
	char firstChar = dict[0][0]; // First char of first string
	// Remove 1st char from string and add remaining string to recurseDict.
	recurseDict.add(dict[0][1 : dict[0].length]);
	for (int i = 1; i < dict.length; ++i) {
		if (dict[i][0] == firstChar) {
			recurseDict.append(dict[i][1 : dict[i].length]);
		} else {
			// Append firstChar to last of chars list.
			chars.append(firstChar);
			// Recurse on sub-dictionary with removed 1st char.
			formDAG(recurseDict, graph);
			recurseDict.clear();
			// Update firstChar and continue loop.
			firstChar = dict[i][0];
			recurseDict.add(dict[i][1 : dict[i].length]);
		}
	}
	// Form edges from the chars list.
	for (int i = 0; i < chars.length; ++i) {
		if (!graph.nodeExists(chars[j]) {
			graph.createIsolatedNode(chars[i]);
		}
		for (int j = i + 1; j < chars.length; ++i) {
			if (!graph.nodeExists(chars[j]) {
				graph.createIsolatedNode(chars[j]);
			}
			graph.getNode(char[i]).addIfNotAlreadyAChild(
				graph.getNode(char[j]));
		}
	}
}

Finally do a topological sort uding DFS on this graph and return the list.

- kartikaditya June 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This wont work always. Suppose I give 2 words: AB and GE (in that order). The only conclusion you can draw is A comes before G. Thats it!! Nothing can be told about B or E. Your solution will print A > G > B > E

- axecapone June 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The graph formed would be node A points to node G. B, E are independent nodes.

May be the confusion was with topological sort. Well, the language may or may not have precedence order amongst all of the literals, but you can achieve partial precedence ordering using topo sort.

If you go with Cormen topo sort algo, he returns a single list and would get what you mentioned A > G > B > E.
I didn't mean that.
I somehow couldn't express the second part more clearly.
So you have a DAG and the interviewer needs ordering, see what you can do...

- kartikaditya June 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Trie, the neglected data structure. It will take care of lexicography.

- Avishek Gurung June 04, 2014 | 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