## Expedia 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

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

``````public void swap(char[] a,int  x,int y)
{
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}

public void permutation(char[] a,int left,int right)
{
if(left == right)
{
for(int i = 0;i < sizeOfArray;i++)
{
System.out.print(a[i] + " ");
}
System.out.print("\n");
}
else
{
for(int currentPos = left;currentPos <= right;currentPos++)
{
swap(a,left,currentPos);
permutation(a,left+1,right);
swap(a,left,currentPos); //Backtracking !
}
}
}``````

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

A intuitive recursive approach:

``````public static ArrayList<String> permutations(String str) {
ArrayList<String> result = new ArrayList<String>();

int length = str.length();
switch(length) {
case 0:
break;
case 1:
break;
case 2:
String rstr = new StringBuffer(str).reverse().toString();
break;
default:
for (int i = 0; i < length; i++) {
String remaining = new StringBuffer(str).deleteCharAt(i).toString();
for (String sub_permutation : permutations(remaining)) {
String s = str.charAt(i) + sub_permutation;
}
}
break;
}

return result;
}``````

Basically for a string "asdf", it first takes 'a' and prepends it to each of the permutations("sdf"), next, it takes 's' & prepends to permutations("adf"), and so on.

Note however that it soon begins to use a lot of memory, and will fail when the string length is more than 10 charecters.

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

main()
{char ch[2],temp;
int i,k=1,b,f;
printf("enter the string");
gets(ch);
for(i=1;i<=strlen(ch);i++)
{ k=k*i; }
f=strlen(ch);
for(i=0;i<k;i++)
{ b=i%(f-1);
printf("%s\n",ch);
temp = ch[b+1];
ch[b+1]=ch[b];
ch[b]=temp;
}

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

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

void swap(char *a, char *b)
{
char tmp = *a;
*a = *b;
*b = *a;
}

void perm(char arr[], int n, int k)
{
int i = 0;

if ((k == n) || (n == 1)) {
printf("%s\n", arr);
return;
}
for(i = k; i < n; i++) {
swap(&arr[i], &arr[k])
perm(arr, n, k+1);
swap(&arr[i], &arr[k]);
}
}

int main()
{
char arr[] = "manoj";
perm(arr, strlen(arr), 0);
return 0;
}``````

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

python version:

``````for i in {}.fromkeys(itertools.permutations("aba"), 0).iterkeys():
print i``````

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

``````# ruby : print all permutations of a string
puts "abcd".chars.to_a.permutation.map { |c| c.join }

# remove uniques
puts ("aba".chars.to_a.permutation.map { |c| c.join }).uniq``````

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

remove the repeat ones .

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

?. that should not produce dups.  ahh. i see. if my string has repeated chars like your example "aba".

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

that's it. Good job!

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

thanks! seems like quite a bit for one line, but it does the trick. :)

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

void Permutation(string beginStr, string endStr)
{
if (endStr.Length <= 1)
{
Console.WriteLine(beginStr+endStr);
}
else
{
for (int i = 0; i < endStr.Length; i++)
{
Permutation(beginStr + endStr[i], endStr.Substring(0, i) + endStr.Substring(i + 1));
}
}
}

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

``````void permute(String str, int l, int r) {

if(l == r-1) {
System.out.println(str);
return;
} else {
for(int i=l; i<r; i++) {
swap(str, l, i);
permute(str, l+1, r);
swap(str, l, i);

}
}

return;
}

void swap(String str, int l, int r) {

char[] charArray = str.toCharArray();
char temp = charArray[l];
charArray[l] = charArray[r];
charArray[r] = temp;
str = new String(charArray);
}``````

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.

### 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.