Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

set<char> findLongestList(vector< pair<char,char> > tuples,vector< pair<char,char> > entry,int start,set<char> final_set)
{
    if(start == entry.size())
    {
        set<char>::iterator it;
        if(tuples.size() >= final_set.size())
        {
            final_set.clear();
            for(int i=0;i<tuples.size();i++)
            {
                final_set.insert(tuples[i].first);
                final_set.insert(tuples[i].second);
            }
        }
        return final_set;
    }
    for(int i=start;i<entry.size();i++)
    {
        if(tuples.empty())
        {
            tuples.push_back( make_pair(entry[i].first,entry[i].second) );
        }
        else
        {
            if(tuples[tuples.size()-1].second == entry[i].first)
            {
                tuples.push_back( make_pair(entry[i].first,entry[i].second) );
            }
        }
        final_set = findLongestList(tuples,entry,start+1,final_set);
        tuples.pop_back(); //backtrack
    }
    return final_set;
}
int main()
{
    set<char> final_set;
    vector< pair<char,char> > tuples;
    vector< pair<char,char> > entry;
    entry.push_back(make_pair('a','b'));
    entry.push_back(make_pair('b','c'));
    entry.push_back(make_pair('c','g'));
    entry.push_back(make_pair('c','d'));
    entry.push_back(make_pair('e','f'));
    entry.push_back(make_pair('d','e'));
    final_set = findLongestList(tuples,entry,0,final_set);
    cout<<"size :"<<final_set.size()<<endl;
    set<char>::iterator it;
    for(it=final_set.begin();it != final_set.end();it++)
        cout<<*it<<"\t";
    cout<<endl;
    return 0;
}

- amit May 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

use size_t instead of int to get rid of compilation warning...

also I found somewhere it's better to use ++i in for loop

for(size_t i=0; i < tuples.size(); ++i)

- zero_cool May 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that there are no possible cycles, my idea would be to solve it using backtracking. The algorithm has a runtime of O(n^n) but due to the search pruning I think it would be ok. Please let me know if someone has a better idea or if I made a mistake.

public int findLongestTupleChain(final List<CharPair> input) {
	int retVal = 0;
	if (input != null && input.size() > 0) {
		retVal = findLongestTupleChain(input, input.get(0), 0) + 1;
	} 
	return retVal;
}


private int findLongestTupleChain(
		final List<CharPair> input, 
		final CharPair levelPair, 
		int level) {
	
	int maxChain = 0;
	CharPair current = null;
	for (int i = 0; i < input.size(); ++i) {
		
		current = input.get(i);
		if (levelPair != current && levelPair.getRight() == current.getLeft()) {
			maxChain = Math.max(level, findLongestTupleChain(input, current, level+1));
		}
	}
	return Math.max(level, maxChain);
}

- Scavi May 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dynamic programming:

dp[i] = Max{For all j<i, dp[i-j]+dp[i] (if comatible), dp[i]}

Maintain a string which finds the maximum so far, return the string at end.

- Amit May 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple DFS will solve this problem. All you need to compute is depth of tree at each level.

- vikas May 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

this problem can be solved via general tree

Based on the input create a general tree and then find out the largest path between root and leaf.

- deadman May 24, 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