Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Phone Interview




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

this is O(n*m): for each character in input string you are making LengthOfPattern comparisons. Probably they wanted to see you implementing KMP but that would be too much for an interview?

- chatbot April 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

change that pattern in place (do not use temp array or variable). KMP, BM etc not applicable

- anvol April 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

KMP - just to find a patter, then use simple shift algorithm.

- Aleksey.M May 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yeah,,, but can u change all appearance of 0,0,3 with complexity O(n).... its above this complexity

- deepak malik April 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how my solution above ? is it okay , seems o(n) to me

- SDguy April 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my version.

Output:

-bash-4.1$ gcc array_replace.c ; ./a.out
BEFORE - 0, 0, 3, 1, 2, 0, 0, 3, 1, 0, 0, 4, 3, 5, 6,
AFTER  - 0, 0, 1, 2, 0, 0, 1, 0, 0, 4, 3, 5, 6,

Below is the code. The complexity seems to be O(n) - we don't parse the array thrice but only once. We go back to previous indices only to replace 003 with 00

(the problem is not as simple as it might seem in the first glance - no forward lookup, need to shift the elements in case any hits (match with 0, 0, 3), since the replacement is less than 3 elements (two elements) - idash variable is used for the purpose.

#include <stdio.h> 
#define DATA_SIZE 15
#define FIND_SIZE 3
#define REPL_SIZE 2

print_array(char *string, int array[], int array_size)
{
	int i = 0; 
	printf("%s - ",string);

	for (i = 0; i < array_size; i++)
	{
		printf("%d, ",array[i]);
	}
	printf("\n");

}
int overwrite(int a[], int index, int repl[], int repl_size)
{
	int i = 0;
	for (i = 0; i < repl_size; i++)
	{
		a[index + i] = repl [i];
	}
}

int replace_array(int a[], int size, int find[], int find_size, int repl[], int repl_size)
{
	
	int i = 0, j = 0, k = 0;
	int findi = 0;
	int broken = 1; 
	int repl_count = 0;
	int idash = 0; 

	for	(i = 0; i < size; i ++, idash ++)
	{
		if (broken)
		{
			findi = 0; 
			broken = 0;
		}
	//	if (repl_count > 0)
		{
			a[idash] = a [i];
		}
		if(a[i] == find[findi])
		{
			findi ++;
			if (findi == find_size)
			{
				overwrite(a, idash - (find_size - 1) , repl, repl_size);
				repl_count ++; 
				//printf("overwriting string find_size:%d, repl_size:%d, \n", find_size, repl_size);
				idash -= (find_size - repl_size); 
				broken = 1;
				
			}
		}
		else
		{
			broken = 1;
		}
	}
	return repl_count; 
}

int main(void)
{

	int data[DATA_SIZE] = {  0, 0, 3, 1, 2, 0, 0, 3, 1, 0, 0, 4, 3, 5, 6};
	int find[FIND_SIZE] = { 0, 0, 3};
	int replace[REPL_SIZE] = { 0, 0};
	int repl_count = 0;
	print_array("BEFORE", data, DATA_SIZE);

	repl_count = replace_array(data, DATA_SIZE, find, FIND_SIZE, replace, REPL_SIZE); 
	print_array("AFTER ", data, DATA_SIZE- repl_count * (FIND_SIZE - REPL_SIZE));
}

- Rams April 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In the code how are you handling the boundary conditions ,suppose the array is ending like 003 then that case what are we going to replace in place of 3.

- Anonymous April 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That particular boundary condition is automatically handled. Only after reaching 3 (after 0, 0) i'm replacing the source array at index idash - (find_size -1).

What I did not handle in the above code a case, where FIND_SIZE is less than REPL_SIZE.

- Rams April 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

array {a1, a2, .... an}, if ak, ak+1, ak+2 is the pattern, replace ak, ak+1 with your wanted number, set ak+2=' ' (space). This is the first iterate.
During the second iterate, once meet space element, move the next element backward one step and so on.
O(n) for each iterate.

- Anonymous April 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is my take.

public static byte[] replace(byte[] thisByteArray)
        {
            int ending = 0;

            for (int i = 0; i < (thisByteArray.Length - ending); i++)
            {
                // make sure no pattern such as 0033
                while (thisByteArray[i] == 0 && thisByteArray[i + 1] == 0 && thisByteArray[i + 		2] == 3)
                {
                    // pull array to front... 
                    for (int j = (i + 2); j < (thisByteArray.Length - ending); j++)
                    {

                        if (j + 1 < thisByteArray.Length - ending)
                        {
                            thisByteArray[j] = thisByteArray[j + 1];
                        }
                    }
                    ending++;
                }
            }
            //retruning a clean array
            byte[] newByte = new Byte[thisByteArray.Length - ending];
            for (int i = 0; i < thisByteArray.Length - ending; i++)
            {
                newByte[i] = thisByteArray[i];
            }
            return newByte;
        }

- Rookie_D April 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

while (i<size-2)
{
if (arr[i]==0 && arr[++i]==0 && arr[++i]==3) arr[i]=0
}

- Shen April 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

quite elegant but how would u know size of resulting array
for example
input array : 1,2,0,0,3,4,5,0,0,3,5,6,7,8,9,0,0,3
ouput expected :1,2,0,0,3,5,6,7,0,0

- Anonymous April 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code replaces 003 to 000, but in the question its mentioned the pattern should be replaced with 00

- s.kalyanasundaram April 03, 2013 | Flag


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