Adobe Interview Question for Software Engineer / Developers






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

This code is on the same logic by which anyone will permute manually. Its intuitive !
Hold one character and permute others, proceed recursively.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void doPermuteString(char in[], char out[], int used[], int length,  int recurselev){

        int i;

        if(recurselev == length){
                printf("%s\n", out);
                return;
        }

        for(i=0;i<length;i++){

                if(used[i])     continue;

                out[recurselev] = in[i];
                used[i] = 1;
                doPermuteString(in, out, used, length, recurselev + 1);
                used[i] = 0;
        }
}

int permuteString(char string[]){

        int i, length, *used;
        char *out;

        length = strlen(string);

        out = (char *)malloc(sizeof(char) * (length + 1));
        out[length] = '\0';

        used = (int *)malloc(sizeof(int) * length);
        for(i=0;i<length;i++){
                used[i] = 0;
        }

        doPermuteString(string, out, used, length, 0);

        free(out);
        free(used);
        return 1;
}

int main(){

        char string[100];
        scanf("%s", string);
        permuteString(string);
        return 0;
}

- Ankit Gupta July 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>
#include<string>
using namespace std;
void permute(string s, int i = 0) {
    if (i == s.size()) cout << s << endl;
    else for (int j = i; j < s.size(); ++j) swap(s[i], s[j]), permute(s, i + 1), swap(s[i], s[j]);
}

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

you r really smart buddy. you made it look like a 2 line solution ;-). must say very elegant solution , excellent demonstration of power of recursion.

- Addy September 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome soln man! :)

- :P December 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution but one small criticism: don't put everything in one line; it is less readable. Nothing wrong with using 3 or 4 more lines to make your code more readable.

- Anonymous January 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Has anyone verified that it even works? i just coded it up to see how it can as it misses lots of detail needed in permutation and actually it does not.

- anon January 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry for the extra newlines...

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void doPermuteString(char in[], char out[], int used[], int length,  int recurselev){

        int i;

        if(recurselev == length){
                printf("%s\n", out);
                return;
        }

        for(i=0;i<length;i++){

                if(used[i])     continue;

                out[recurselev] = in[i];
                used[i] = 1;
                doPermuteString(in, out, used, length, recurselev + 1);
                used[i] = 0;
        }
}

int permuteString(char string[]){

        int i, length, *used;
        char *out;

        length = strlen(string);

        out = (char *)malloc(sizeof(char) * (length + 1));
        out[length] = '\0';

        used = (int *)malloc(sizeof(int) * length);
        for(i=0;i<length;i++){
                used[i] = 0;
        }

        doPermuteString(string, out, used, length, 0);

        free(out);
        free(used);
        return 1;
}

int main(){

        char string[100];
        scanf("%s", string);
        permuteString(string);
        return 0;
}

- Ankit Gupta July 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why doesn't the formatting works properly ?? huh

- Ankit Gupta July 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are bugs with the formatting, if you use empty lines.

Try using spaces instead of empty lines.

Example:

Empty line:

a

    b

Empty line with spaces:

a
    
    b

- LOLer July 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what's the time complexity ?

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

@anonymous ...your solution is the best/smartest one...rest sucks..why the hell people like writing pages and pages of stuffs when it can be done in just 2 lines.... anony, u rock buddy. thanks for the nice solution!

- googler August 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String can contains repeat chars.

- Lord Darth Plagueis August 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Mr Ankit Gupta has plaigarised the code from programming interviews exposed.. i hope no one catches him:

- arh August 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@arh are u an asshole ? no one ever said that whatever code you write has got to be a unique code in the world. You learn things from somewhere and if you like, you will reproduce it somewhere else...

- Anonymous October 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The point is that *if* he took it from some source he should acknowledge it, otherwise it's IP theft.

- Anonymous January 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't know who is right but you should show some courtesy by mentioning the source from where you collected it. It would look like it to be your own solution. Other people in past threads did the same (mentioning the source).

- Anonymous February 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the 2 line is a extra ordinary code.r u in adobe ;)

- nikhil November 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't know but 2 line code doesn't seem to work :( I tried with swap pass by value.

- Anonymous December 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

off, you need to pass by reference.

- Anonymous December 29, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

May be its duplicate...but its tested

void swap(int *ar, int pos1, int pos2);
void permute(int *ar, int start, int ar_len);
main()
{
int ar[]={1,2,3,4,5,6,7,8}; /*Just as an example */
int len = 8;
permute(ar, 0, len);
return 0;
}


void swap(int *ar, int pos1, int pos2)
{
int temp = 0;
temp = ar[pos1];
ar[pos1]= ar[pos2];
ar[pos2] = temp;
}

void permute(int *ar, int start, int ar_len)
{
int i = 0, j = 0;
if (start == (ar_len-1)) {
while(i<ar_len){
printf("%d",ar[i++]);
}
printf("\n");
}
for (i=start; i<=(ar_len-1);i++) {
swap(ar, start, i);
j = 0;
permute(ar, start+1, ar_len);
swap(ar, start, i);
}
}

- Anonymous January 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if the original string has duplicate characters? how do you generate/print out unique strings without caching previous string in memory?

- Itcecsa June 25, 2011 | 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