Interview Question


Country: India




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

import java.util.*;
class PrintAllSorted 
{
	public static void main(String[] args) 
	{
		List<String> strs = new ArrayList<String>();
		strs.add("abc");
		strs.add("def");
		strs.add("yz");
		PrintSorted(strs, "", 0);
	}
	public static void PrintSorted(List<String> strs, String currentStr, int focusListIndex)
	{
		//check if more str in list to proceed
		if(focusListIndex==strs.size())
		{
			System.out.println(currentStr);
		}
		else
		{
			char lastChar = (char)0;
			if(currentStr.length()>0)
				lastChar = currentStr.charAt(currentStr.length()-1);
			String tempStr = strs.get(focusListIndex);
			for(int i=0; i<tempStr.length();i++)
			{
				//check each char at ith string fits the scenario
				if(tempStr.charAt(i)>lastChar)//can be candidate
				{
					PrintSorted(strs, currentStr+tempStr.charAt(i), focusListIndex+1);
				}
			}
		}
	}
}

- Anonymous March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hey nice approach.
Your o/p works if we take input in the sorted order.
abc,def,yz

Anyways, We can sort the strings before calling the PrintSorted func.

Can you suggest me a good algo which can sort strings( in general) and also in this case where only the first char in each string is sufficient to sort the strings.
e.g.
I/P: DEFG YZ ABC
O/P: ABC DEFG YZ

- vikdp01 March 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi vikdp01,

This is question is not sufficient to understand. Is it compulsory to take only one character from each strings.

- mw_free1 March 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes .. we can modify the soln from anonymous ...

- vikdp01 March 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I am writing the algo for this

Take a hash which will look like this [Assuming the input given in in sorted order]

A->B->C->D
D->E->F
Y->Z

Now we can take the first element of every row
ADY
Increment last row by one and carry on like
ADZ
AEY
AEZ


Please let me know if I am doing something wrong here

- DashDash April 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<string> get_sorted(vector<vector<int>>& lists)
{
	vector<string> vec;
	vector<int> index;
	for(size_t i = 0; i < lists.size(); ++i)
	{
		if(lists[i].size() == 0)
		{
			return vec;
		}

		int elt = list[i][0];

		vector<int>::iterator it = list[i].begin();
		while(it != list[i].end() && elt >= list[*it][0])
		{
			++it;
		}

		index.insert(it, elt);
	}

	string str;
	for(size_t i = 0; i < index.size(); ++i)
	{
		str += list[index[i]].pop();
	}
	
	vec.push_back(str);	
	vector<string> strs = get_sorted(lists);
	for(size_t i = 0; i < strs.size(); ++i)
	{
		vec.push_back(strs[i]);
	}
	
	return vec;
}

- Anonymous July 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// question is nothing but printing the cross-products

void cross_product(vector<string>& strs)
{
          int p  = 1;
          for (int i=0; i < strs.size(); i++) p *= strs[i];
          for (int i=0; i < p ; i++)
          {
                   
                    int j = p; 
                    for (int k=0; k < strs.size(); k++)
                    {
                                    int s = strs[k].size();
                                    j /= s; 
                                    cout << strs[j][ (i/j)%s ] << endl; 
                    }
          }
}

- Anonymous August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// question is nothing but printing the cross-products

void cross_product(vector<string>& strs)
{
          int p  = 1;
          for (int i=0; i < strs.size(); i++) p *= strs[i];
          for (int i=0; i < p ; i++)
          {
                   
                    int j = p; 
                    for (int k=0; k < strs.size(); k++)
                    {
                                    int s = strs[k].size();
                                    j /= s; 
                                    cout << strs[j][ (i/j)%s ] << endl; 
                    }
          }
}

- Anonymous August 02, 2013 | 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