Interview Question


Country: United States




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

Here's a simple code in C++.

Iterate over the permuted string and find the first position in original string which is equal to the current character of permuted. Then go back and swap one by one.

O(n^2)

void Swapping(string S,string T)
{

	for (int i=0;i<S.size();i++){
		int j=i;

		while (S[j]!=T[i])j++;
		while (j>i){
			swap(S[j],S[j-1]);
			cout << S << endl;
			j--;
		}
	}

}

- MehrdadAP June 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think it's optimal, but it's definately compact. I'd hire you.

- zortlord June 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@zortlord: I think my code does the minimum number of steps to get the permuted string, although I can't prove it yet.

It seems to me, that in worst case we need O(n^2) number of steps to make the permuted one.

Can you do the following example better than 10 steps?

ABCDE
EDCBA

- MehrdadAP June 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think Mehrdad is right.Due to the restrictions imposed in the question, this seems to be an optimal solution.

- Klaus July 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int search(string src, int s, char ch)
{
    for(int i=s;i<src.length();i++)
    {
        if(src[i] == ch)
            return i;
    }
    return -1;
}           
    
void pathSrc2Dst(string src, string dst)
{
    int lenS = src.length();
    int lenD = dst.length();
    
    if(lenS != lenD)
    {
        cout<<"  Invalid Input "<<endl;
        return;
    }
    
    int d = 0, s = 0;
    cout<<"  "<<src;
    while(true)
    {
        int loc = search(src, s, dst[d]);
        if(loc<0)
        {
            cout<<"  Invalid Input "<<endl;
            return;
        }
        for(int i=loc;i>s;i--)
            swap(src[i], src[i-1]);   
        s++;
        d++;
        cout<<" ----> "<<src;
        if(s == src.length() || src == dst)
            break;
    }
    cout<<endl;
}

int main()
{
    string src, dst;
    
    cout<<"  Enter source      :- ";
    cin>>src;
    
    cout<<"  Enter destination :- ";
    cin>>dst;
    
    pathSrc2Dst(src, dst);
    return 0;
}

- skum June 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bubble sort!!

- Montreal6 June 27, 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