Microsoft Interview Question for Software Engineer / Developers


Team: AppeX
Country: India
Interview Type: In-Person




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

Here is my code for the odoMeter. You only need to map the odoValue to the cartesian product you want.

int odoNext(int *pOdoMax, int len, int* pOdoValue)
{
        int inc;
        if(NULL==pOdoMax || NULL==pOdoValue || 1>len)
                return -1;
        len=len-1;
        inc=1;
        for(;len>=0 && inc;len--)
        {
                pOdoValue[len]=(pOdoValue[len]+inc)%pOdoMax[len];
                inc=!pOdoValue[len];
        }
        return (inc&&(len<0));
}

void odoInit(int *pOdoValue, int len)
{
        memset((void*)pOdoValue, 0, sizeof(int)*len);
}

void odoPrint(int* pOdoValue, int len)
{
     int count;
     printf("Odo Print :\n");
     for(count=0;count<len;count++)
        printf("%d ", pOdoValue[count]);
     printf("\n");
}

- chenlc626 February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Test Logic

{
        int odoMax[4]={2,3,4,2};
        int odoValue[4];

        odoInit(&odoValue, 4);
        while(1)
        {
                odoPrint(&odoValue,4);
                if(0!=odoNext(&odoMax, 4, &odoValue))
                        break;
        }

}

- chenlc626 February 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Test Result:

Odo Print :
0 0 0 0 
Odo Print :
0 0 0 1 
Odo Print :
0 0 1 0 
Odo Print :
0 0 1 1 
Odo Print :
0 0 2 0 
Odo Print :
0 0 2 1 
Odo Print :
0 0 3 0 
Odo Print :
0 0 3 1 
Odo Print :
0 1 0 0 
Odo Print :
0 1 0 1 
Odo Print :
0 1 1 0 
Odo Print :
0 1 1 1 
Odo Print :
0 1 2 0 
Odo Print :
0 1 2 1 
Odo Print :
0 1 3 0 
Odo Print :
0 1 3 1 
Odo Print :
0 2 0 0 
Odo Print :
0 2 0 1 
Odo Print :
0 2 1 0 
Odo Print :
0 2 1 1 
Odo Print :
0 2 2 0 
Odo Print :
0 2 2 1 
Odo Print :
0 2 3 0 
Odo Print :
0 2 3 1 
Odo Print :
1 0 0 0 
Odo Print :
1 0 0 1 
Odo Print :
1 0 1 0 
Odo Print :
1 0 1 1 
Odo Print :
1 0 2 0 
Odo Print :
1 0 2 1 
Odo Print :
1 0 3 0 
Odo Print :
1 0 3 1 
Odo Print :
1 1 0 0 
Odo Print :
1 1 0 1 
Odo Print :
1 1 1 0 
Odo Print :
1 1 1 1 
Odo Print :
1 1 2 0 
Odo Print :
1 1 2 1 
Odo Print :
1 1 3 0 
Odo Print :
1 1 3 1 
Odo Print :
1 2 0 0 
Odo Print :
1 2 0 1 
Odo Print :
1 2 1 0 
Odo Print :
1 2 1 1 
Odo Print :
1 2 2 0 
Odo Print :
1 2 2 1 
Odo Print :
1 2 3 0 
Odo Print :
1 2 3 1

- chenlc626 February 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

What is the relation between odometer code and cartesian product? I'm not able to see it, can someone please explain.

- Anonymous February 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Still Use Odometer

import java.util.ArrayList;
import java.util.List;

public class Cartesian {

	public static void generateCartesian(String[] strs) {
		int K = strs.length;

		int[] sizes = new int[K];
		for (int i = 0; i < sizes.length; i++) {
			sizes[i] = strs[i].length();
		}

		List<Integer> odometer = new ArrayList<Integer>();
		for (int index = 0; index < K; index++) {
			odometer.add(index, 0);
		}

		List<Integer> max_indexes = new ArrayList<Integer>();
		for (int index = 0; index < K; index++) {
			max_indexes.add(index, sizes[index]);
		}

		while (true) {
			// output based on current odometer
			for (int index = 0; index < odometer.size(); index++) {
				int pos = odometer.get(index);
				System.out.print(strs[index].charAt(pos));
			}
			System.out.println();
			// advance odometer
			if (!advance_odometer(odometer, max_indexes)) {
				break;
			}
		}
	}

	private static boolean advance_odometer(List<Integer> odometer,
			List<Integer> max_indexes) {
		int i = odometer.size() - 1;
		while (i >= 0) {
			odometer.set(i, odometer.get(i) + 1);
			if (odometer.get(i) < max_indexes.get(i)) {
				return true;
			} else {
				// roll over to next index
				odometer.set(i, 0);
				i = i - 1;
				if (i < 0) {
					return false;
				}
			}
		}
		return false;
	}

	public static void main(String[] args) {
		String[] strs = { "ABCD", "123", "ZX" };
		generateCartesian(strs);
	}

}

- Kevin February 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//*a[] = k diff arrays.
char * cart(char *a[], int start, int end)
{
int mid;
if(start==end)
{
return a[start];
}
else
{ mid=(start+end)/2;
char *a1=cart(a, start, mid);
char *b1=cart(a, mid+1, end);
return(product(a1,b1));
}
}
char * product(char *a1, char *b1)
{
// return cartesian product of a1,b1 in a char *product;
}

- ameyabap February 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is your measure to acheive optimality here ? Divide and conquer is as good as blindly performing the product in any order. As per my understanding, the arrays should be taken in increasing order of their length and perform cartesian product sequentially; this would perform minimum number of unit level production (i.e. character appending) as well as minimum traversal over generated strings.

- Russell February 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

An optimal solution should produce the next string with O(k) operations. It takes a minimum of k operations to persist the letters of the string, so you can't do better than O(k), and determining where to get each letter should be constant time.

The total running time should be proportional to k times the product of all the string lengths.

I like to use the odometer approach here, because it's very straightforward to analyze the running time.

- showell30@yahoo.com February 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python recursive solution:

def cartesian(lst):
    if not lst: return
    def f(i, prefix):
        if i == len(lst) - 1:
            for c in lst[i]:
                yield prefix + c
        else:
            for c in lst[i]:
                for s in f(i+1, prefix+c):
                    yield s

    for s in f(0, ''):
        yield s

strings = [
    'abcd',
    '123',
    'XY'
]
for s in cartesian(strings):
    print s

- showell30@yahoo.com February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python Odometer Solution:

Using an odometer-based approach is arguably overkill for this problem, but it's a good technique to have in your bag of tricks. An odometer function yields the cartesian product of a bunch of integer ranges. It comes up in other problems. For example, suppose you have a prime factorization for a number, and you want to generate all the composite factors of the number. You can use an odometer to generate all the combinations of exponents.

def test():
    strings = [
        'abcd',
        '123',
        'XY'
    ]
    for s in cartesian(strings):
        print s

def cartesian(strings):
    max_indexes = map(len, strings)
    for indexes in odometer(max_indexes):
        letters = [strings[i][j] for i, j in enumerate(indexes)]
        yield ''.join(letters)

def odometer(max_indexes):
    odometer = [0] * len(max_indexes)
    def advance_odometer():
        i = len(odometer) - 1
        while i >= 0:
            odometer[i] += 1
            if odometer[i] < max_indexes[i]:
                return True
            else:
                # roll over to next index
                odometer[i] = 0
                i -= 1
                if i < 0:
                    return False
    while True:
        yield odometer
        if not advance_odometer():
            break
test()

- showell30@yahoo.com February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one. You might be interested in www . careercup . com/ question?id=15419952

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

I think. If there are k arrays of different lengths (l1,l2...lk) then best case complexcity can not be less than O(l1*l2...lk), because we need to generate all at least such results. lets say if we have 3 char arrays of size 2,3,4 then result should contain 2*3*4=24 strings. so here we can use bottom to top approach. algo should be:
1- mutliply k & k-1 char arrays
2- multiply results of step 1 to k-2
3-and so on till k=1.

As per our example. k= 3, l1= 2, l2=3;l3 =4
l3(3)*l(4)
\ /
l(12)*l(2)
\ /
l(24)

- talk2arpit March 01, 2013 | 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