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);
}

- zdmytriv March 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

beautiful !!!!

- usafzz May 21, 2008 | Flag
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?

- sppl October 04, 2008 | Flag
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;
		}
	}

- kg January 15, 2007 | Flag Reply
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.

- Jack January 19, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous March 06, 2007 | Flag
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);
}

- Billy June 17, 2007 | Flag Reply
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.

- zdmytriv March 11, 2008 | Flag Reply
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) {
result.Add(seed + o);
}
result.Add(seed);
combination(input, result);
}

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

- sppl October 04, 2008 | Flag Reply
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;
}

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

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.

- Rex March 09, 2012 | Flag
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))

- Adi September 05, 2012 | 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