## Expedia Interview Question for Software Engineer / Developers

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

"Present" string as binary. If i-th bit ON print character i.

L - length of the string;
2^(L-1) - number of combinations

``````void combinations(const char * string)
{
int n = strlen(string); // number of characters in the string
long c = pow((float)2, n); // number of combinations
for(int i = 1; i <= c; ++i) {
int tmp = i;
int index = 0;
while(tmp > 0) {
if(tmp & 1) { printf("%c", string[index]); }
tmp >>= 1;
++index;
}
printf("\n");
}
}

int main()
{
char string[12] = "abcdefghxyz";
combinations(string);
}``````

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

beautiful !!!!

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

No doubt this is an elegant solution. But does n't this have a limitation based on number of bits in the datatype. tmp is a int. There can only be 32 bits in the int. What about strings 100 characters long?

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

Assumptions: Input is String instead of char array.
Code is in Java:

``````public static void printAllSubsets(String s){
int[] base=new int[s.length()];
String s1="";
while(!s1.equals(s)){
s1="";
nextCombination(base);
for(int i=0;i<base.length;i++){
if(base[i]==1){
s1=s1+s.charAt(i);
}
}
System.out.println(s1);
}
}

public static void nextCombination(int[] arr){
int i=arr.length-1;
while(i>=0){
if(arr[i]==0){
break;
}
i--;
}
if(i<0){
return;
}

arr[i++]=1;
while(i<arr.length){
arr[i++]=0;
}
}``````

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

I think one approach that may work is dynamic programming. If you do it bottom-up, you can find all combinations. However, if I'm not mistaken, DP is usually O(n^2) or worse, and doing it iteratively is a downhill compilation task.

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

You can represent each combination as a binary number with n bits. If the bit is on, the letter in that index is included in the combination. For a string of length n there are 2^(n+1)-1 combinations (if you include the empty set as a subset), therefore I don't think you can do it in less than 2^(n+1) time. I think that's the idea behind kg's response.

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

Ouput: x1...xn where xi denotes whether the i-th element of the input appears in the subset or not

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

void gen(int* input, int n, int index);

int main(int argc, char* argv[])
{
int *input;
int n;

if (argc != 2) {
printf("Usage: %s <#elements>\n",argv[0]);
exit(0);
}

n = atoi(argv[1]);

input = malloc(sizeof(int)*n);
gen(input,n,0);
}

void gen(int* input, int n, int index)
{
int i;

if (index == n) {
for (i = 0; i < n; i++)
printf("%d",input[i]);
printf("\n");
return;
}

input[index] = 0;
gen(input,n,index+1);
input[index] = 1;
gen(input,n,index+1);
}``````

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

For the case "abab". When required to find unique substrings then just build hash with substring as key and append only unique substrings.

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

Yet another solution,

public static void combination(Queue<String> input,List<String> result ) {
if (input.Count == 0) return;
string seed = input.Dequeue();
List<String> copy = new List<string>(result);
foreach (String o in copy) {
}
combination(input, result);
}

Take each character in the input character array and put it in a Queue. Pass this queue to the above method.

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

#include <stdio.h>
#include <math.h>

int main() {
int a[] = {1,2,3,4,5}, size;
size = sizeof(a)/sizeof(a[0]);
for (int i = 0; i < pow(2.0, size); ++i) {
printf("{");
for (int j = 0; j < size; ++j)
if (((i >> j) & 1) == 1)
printf(" %d ", a[j]);
printf("}\n");
}
return 0;
}

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

No need to use pow() function even..If it is all about power of two than Why not left/right shift only so instead of pow(2.0, size) we can use 1<<size.

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

``````def subsets(a):
if a == None or a == []:
return [set([])]
r = subsets(a[1:])
t = []; t += r
for i in r:
t += [i | set([a[0]])]
return t

print subsets(range(3))``````

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.