Adobe Interview Question for Software Engineers


Country: United States




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

The approach is simple:
match the head and for the tail do a swap for desired character to left.

I could not prove it would be lease

#include <iostream>
#include <map>
#include <cstring>
using namespace std;

void swap(char * convert , int j)
{
	char temp = convert[j];
	convert[j] = convert[j-1];
	convert[j-1] = temp;
}

int transformCost(char * master, char * convert , int i)
{
	int matchcounter = 0;
	int cost = 0;
	while(matchcounter < i)
	{
		if(master[matchcounter] == convert[matchcounter])
		{
			matchcounter++;
			continue;
		}
		char m = master[matchcounter];
		bool found = false;
		int matchpos = -1;
		for(int j = matchcounter ; j < i; j++)
		{
			if(convert[j] == m)
			{
				matchpos = j;
				found = true;
				break;
			}
		}
		if(found)
		{
			swap(convert,matchpos);
			cost++;
		}
		else
		{
			return -1;
		}
	}
	cout<<"Master  : " << master<<endl;
	cout<<"Convert : " << convert<<endl;
	return cost;
}


int main()
{
	char a[] = "101010101010";
	char b[] = "111111000000";
	cout<<transformCost(a,b, strlen(a))<<endl;
	return 0;
}

- kingKode March 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you provide reasoning for it?

- Skor March 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int SwapCount(int A[], int B[], int size)
{
	int j = 0;
	int minSwapCount = 0;
	for(int i =0; i < size; i++)
	{
		if(A[i] == 1)
		{
			
			while(B[j] !=1)
			{
				j++;
			}
			if(i > j)
				minSwapCount += i - j;
			else
				minSwapCount += j - i;
			j++;
		}
	}

	return minSwapCount;

}
int main()
{
	int A[] = {1,0,0,1};
	int B[] = {0,1,1,0};
	printf("SwapCount = %d", SwapCount(9, 6));
	return 0;
}

- puneetgupta101 May 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int minSwaps(int A[], int B[], int n)
{
	assert(A != NULL && B != NULL && n > 0);

	int ans = 0, rb = 1;

	for (int i = 0; i < n; ++i)
	{
		if (A[i] != B[i])
		{
			if (rb <= i) rb = i + 1;

			while (rb < n && A[rb] != B[i]) ++rb;

			if (rb >= n) throw logic_error("there is no solution");

			ans += rb - i; A[rb] = A[i]; ++rb;
		}
	}

	return ans;
}

- malinbupt May 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void findMInSwapBinary(int binArr1[n],int binArr2[n])
{
	int minSwaps =0;
	for(int i=0;i<n;i++)
	{
		if(binArr1[i] ^ binArr2[i]==1)
			minSwaps++;
	}
	return;
}

- sv March 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Your logic does not work on this.
eg. arr1[]={1,1,1,0,0,0}
arr2[]={1,0,1,0,1,0}
You output would result 2
Correct answer is 3.

- GAURAV March 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

correct answer is 2.
Only 2 swaps are required.
can u explain why it's 3?

- sv March 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

sv, I think you are missing the part that says you can only swap adjacent elements.

- gudujarlson March 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

O(1) solution .....

int numOfSwaps(int x, int y){
 int tmp = x ^ y;
 return NumberOfSetBits(tmp);
}

int NumberOfSetBits(int i)
{
     i = i - ((i >> 1) & 0x55555555);
     i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
     return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}

- Anonymous March 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

O(1) solution ....

int countSwaps(int x, int y){
		int tmp = x ^ y;
		return numberOfSetBits(tmp);
	}
	
	int numberOfSetBits(int i)
	{
	     i = i - ((i >> 1) & 0x55555555);
	     i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
	     return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
	}

- Ajeet March 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Ooops submitted two times, pls ignore first one.

- Ajeet March 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sv, The answer would have been 2 if we were not given the condition of adjacent swapping only. Since we can only swap two adjacent elements, minimum swaps will be 3.

- GAURAV March 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, thats right..i missed that condition...

The solution I have given would work without swapping.
we can xor the numbers and find the bits which are set in the result.
We can just toggle those bits in the respective arrays for converting them.

- sv March 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void findMInSwapBinary(int binArr1[n],int binArr2[n])
{
	int minSwaps =0;
	for(int i=0;i<n;i++)
	{
		if(binArr1[i] ^ binArr2[i]==1)
			minSwaps++;
	}
	return;

}

- sv March 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

above function is wrong. For example in case of 1,0,0,1 & 0,1,1,0, this function will return 4 but the answer should be 2.

- Puneet May 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

XOR the numbers, then see how many 1s there are in this value

Reasoning:
We know that a ^ a = 0 , so 1 ^ 1 = 0 and 0 ^ 0 = 0
But, 1 ^ 0 = 1 and 0 ^1 = 1, so we see that we can find differences by xoring


Some code:

int numSwaps(int a, int b) {
	
	int c = a ^ b;
	int output = 0;

	while (c != 0) {

		out += c & 1;
		c =>> 1;
	}

	return out;
}

- Skor March 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This one works, but given the answer size there might be an easier solution or this is not an interview question.

public static void main(String[] args) throws Exception {
        Random rseed = new Random();
        int seed = rseed.nextInt(100);
        System.out.println("seed = " + seed);
        Random r = new Random(seed);
        int size = 10;
        int[] orig;
        int[] dest = new int[size];
        for (int i = 0; i < dest.length; i++) {
            dest[i] = r.nextInt(3)==1? 1 : 0;
        }
        orig = dest.clone();
        for (int i = 0; i < dest.length-1; i++) {
            int tmp = orig[i];
            int destIndx = i + r.nextInt(size-i-1);
            orig[i] = orig[destIndx];
            orig[destIndx] = tmp;
        }

//        orig = new int[]{1, 0, 0, 0, 1, 0, 0, 0, 0, 1};
//        dest = new int[]{0, 0, 0, 0, 0, 1, 0, 1, 0, 1};

        System.out.println("Orig" + Arrays.toString(orig));
        System.out.println("Dest"+Arrays.toString(dest));
        System.out.println();
        System.out.println("Best "+findMinSwaps(orig, dest));


    }

    private static int findMinSwaps(int[] orig, int[] dest) throws Exception {
        int leftDest=0,
                leftOrigin=0,
                rightOrigin=orig.length-1,
                rightDest=orig.length-1;

        int swaps = 0;
        while(!Arrays.equals(orig,dest)){
            //scan left to right
            while(leftDest < dest.length && (dest[leftDest]==0 || orig[leftDest]==1) )
                leftDest++;
            while(leftOrigin< orig.length && (orig[leftOrigin]==0 || dest[leftOrigin]==1))
                leftOrigin++;
            if(leftDest != dest.length-1 && leftOrigin != dest.length-1 )
                swaps+= multipleSwaps(orig,leftOrigin,leftDest);

            //scan right to left
            while(rightDest>0 && (dest[rightDest]==0 || orig[rightDest]==1))
                rightDest--;
            while(rightOrigin>0 && (orig[rightOrigin]==0 || dest[rightOrigin]==1) )
                rightOrigin--;
            if(rightDest != 0 && rightOrigin != 0)
                swaps+= multipleSwaps(orig,rightOrigin,rightDest);

            leftDest++;
            leftOrigin++;
            rightDest--;
            rightOrigin--;
        }
        return swaps;
    }

    private static int multipleSwaps(int[] orig, int from, int to) throws Exception {
        if(from == to)
            return 0;
        int direCtion = (to-from)/Math.abs(to-from);
        int swaps =0;
        while(from != to){
            if(orig[from+direCtion]==1){
                swaps+=multipleSwaps(orig,from+direCtion,to);
                swap(orig,from,from+direCtion);
                System.out.println("Orig" +Arrays.toString(orig));
                return swaps+1;
            } else {
                swap(orig,from,from+direCtion);
                System.out.println("Orig" +Arrays.toString(orig));
                from+=direCtion;
                swaps++;
            }
        }
        return swaps;
    }


    static void swap(int[] data, int indx1, int indx2) throws Exception {
        if(Math.abs(indx1-indx2)!= 1)
            throw new Exception("Wrong indexes " + indx1 + " " + indx2);
        int tmp = data[indx1];
        data[indx1] = data[indx2];
        data[indx2] = tmp;
    }

- Anonymous March 31, 2015 | 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