Microsoft Interview Question for Testing / Quality Assurances


Country: India
Interview Type: In-Person




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

C:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char *str="ABCDEFG";  //test subject 
    int N = strlen(str);  //length of it

    int resultN = 4*(N-1); //length result string (-1 excludes last char)
    char *result = malloc( resultN + 1 ); //space for result string 

    //check malloc failure if you desire

    //4 positions of result corresponding to every char of str
    int a,b,c,d;  

    int i;
    for( i=0; i < N-1; i++)
    {
        /* first two positions in first half of result array */
        a=i << 1; // 2*i       (first position)
        b=a+1;    // 2*i+1     (second)

        /* last two positions in last half of result array */    
        c=resultN-1-a;  //resultN - 2*i -1  (third)
        d=c-1;          //resultN - 2*i -2  (fourth)

        /* fill the result positions in with current input character */
        result[a]=result[b]=result[c]=result[d]= str[i];
    }

    result[resultN] = '\0'; //NUL terminate

    printf("%s\n", result);

    return 0;
}

- S O U N D W A V E October 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If I go by the example and logic involved, then output will always be 4*first character. Correct me if I am wrong in understanding the question, or you can give one contradicting example.

- OTR October 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

4*first letter? That would have been 1 line of code.

printf ("%c%c%c%c\n", str[0],str[0], str[0], str[0]);

- S O U N D W A V E October 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

As I understand, the expected output is AABBCCCCBBAA for the input 'ABCD', but why the question talks about second and third iteration?

that will be a separate routine if 'ABC' is passed as input, the expected output is AABBBBAA

and same is with the third routine with the input as 'AB' and the expected output as AAAA

- siva October 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 8 vote

public class Main {
    public static void main(String[] args) {
        String str = "ABCD";

        StringBuilder builder;
        for (int i = 0; i <= str.length()-1; i++) {
            builder = expand(str.substring(0, str.length()-i));
            System.out.println("final == " + builder.toString()+ builder.reverse().toString());
        }
    }

    public static StringBuilder expand(String str) {
        StringBuilder builder = new StringBuilder();

        for(char ch:str.toCharArray()) {
            builder.append(duplicate(ch));
        }

        return builder;
    }

    public static String duplicate(char ch) {
        return  "" + ch + ch;
    }
}

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

Good solution . Except you missed the - "excluding the last character. "

- vickykansal October 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This only takes care of the first iteration -- AABBCCCCBBAA ( after a minor change) - what about other iterations ?>

- Jai October 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

String str = "ABCD";
StringBuffer buffer = new StringBuffer();
for(int i = 0 ; i < str.length()-1; i++) {
buffer.append(str.charAt(i));
buffer.append(str.charAt(i));
}
System.out.println("final ==" + buffer.toString()+ buffer.reverse().toString());

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

def callFunct():
	strDup(list("ABCD"),0)

def strDup(ls1,length):
	while length < len(ls1):
		str1 =""
		tempList = list()
		for chars in ls1[:-length]:
			str1=str1+chars+chars
			tempList.append(chars)
		for chars in tempList:
			temp = tempList.pop()
			str1 = str1+temp+temp
		print str1
		strDup(ls1, length+1)

## Something like this in python... pardon few errors here and there didn't get time to compile

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

#include "stdafx.h"
#include <iostream>
#include <stack>
#include <cstdlib>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string testCase = "ABCD";
	stack<string> stc;
	string b = "";
	for (int i = 0; i < testCase.length(); i++)
	{
		b = b + testCase.at(i);
		stc.push(b);
	}
	while (stc.size() > 0)
	{
		string t = stc.top();
		int j = 0;
		for (int i = 0; i < t.length(); i = j / 2)
		{
			cout << t.at(i);
			j++;
		}
		int k = 0;
		for (int i = 0; i < t.length(); i = k / 2)
		{
			cout << t.at(t.length() - 1 - i);
			k++;
		}
		cout << endl;
		stc.pop();
	}
	system("pause");
	return 0;
}

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
void MyPrint(char * strdes)
{
	int N = (strlen(strdes) - 1) * 4 + 4 + 1; 
	char* strsrc;
	int index = 0;	
	if(NULL == (strsrc =(char *) malloc (sizeof(char) * N)))	
	{
		perror("not enough memeory!");
		exit(1);
	}

	for(int i=0; i< strlen(strdes)-1; i++)
	{
		for(int j=0; j<2; j++)
			strsrc[index++] = strdes[i];
	}

	
	for(int j=0; j<4; j++)
		strsrc[index++] = strdes[strlen(strdes)-1];
	

	for(int i= strlen(strdes)-2; i>=0; i--)
	{
		for(int j=0; j<2; j++)
			strsrc[index++] = strdes[i];
	}
	
	strsrc[index] = '\0';
	
	printf("%s\n", strsrc);
	free(strsrc);
}

int main()
{
	char* s = "ABC";
	MyPrint(s);
	
}

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

M=len(String)-1;
N=4*M;
for(i=0,j=0 ; i<M,j<N ; i++,j+=2)
a[j]=a[i];

j=2;i=0;
while(i<N)
{
while(i<j)
{
b[i]=b[n-i]=a[j];
i++;
}
j+=2;
}

for(i=0; i<N ; i++)
cout<<b[i]

- Sidz October 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringDubs
{
class Program
{

static void Dubs(string word)
{
if (word.Length>0)
{
int k = 0;
int i = 0, j = 0;
char c;
char[] mani = new char[(word.Length) * 2];
for (i = 0; i < word.Length; i++)
{
for (j = 0; j < 2; j++)
{
mani[k] = word[i];
k++;
}


}
word = word.Remove(word.Length-1);
for (k = 0; k < mani.Length; k++)
{
Console.Write(mani[k]);
}
Array.Reverse(mani);
for (k = 0; k < mani.Length; k++)
{
Console.Write(mani[k]);
}

Console.WriteLine();
}
Dubs(word);
}
static void Main(string[] args)
{
Dubs("ABC");
}
}
}

- Qardahji October 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringDubs
{
    class Program
    {

        static void Dubs(string word)
        {
            if (word.Length>0)
            {
                int k = 0;
                int i = 0, j = 0;
                char c;
                char[] mani = new char[(word.Length) * 2];
                for (i = 0; i < word.Length; i++)
                {
                    for (j = 0; j < 2; j++)
                    {
                        mani[k] = word[i];
                        k++;
                    }

                    
                }
                word = word.Remove(word.Length-1);
                for (k = 0; k < mani.Length; k++)
                {
                    Console.Write(mani[k]);
                }
                Array.Reverse(mani);
                for (k = 0; k < mani.Length; k++)
                {
                    Console.Write(mani[k]);
                }

                Console.WriteLine();
            }
            Dubs(word);
        }
        static void Main(string[] args)
        {
            Dubs("ABC");
        }
    }
}

- Qardahji October 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
String str = "ABCD";

StringBuilder builder;
for (int i = str.length() - 1; i > 0; --i) {
builder = expand(str.substring(0, i));
System.out.println("final == " + builder.toString()+ builder.reverse().toString());
}
}

public static StringBuilder expand(String str) {
StringBuilder builder = new StringBuilder();

for(char ch:str.toCharArray()) {
builder.append(duplicate(ch));
}

return builder;
}

public static String duplicate(char ch) {
return "" + ch + ch;
}

- akshay October 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have improved the solution provided by Urik Lagnes


class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the string");
string input = Console.ReadLine();

int displayLength = input.Length - 1;

while (displayLength > 0)
{
char[] displayString = new char[displayLength * 4];

for (int i = 0; i < displayLength; i++)
{
displayString[(i << 1)] = input[i];
displayString[(i << 1) + 1] = input[i];
displayString[(displayLength * 4) - (i << 1) - 1] = input[i];
displayString[(displayLength * 4) - (i << 1) - 2] = input[i];
}
Console.WriteLine(displayString);
displayLength -= 1;
}

}
}

- coder123 October 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *s="abcd";
 int cnt=strlen(s)-1;
 int n=strlen(s);
 int i,j,index=0;
 char *str;
  while(cnt>=1)
 {
  index=0;
  for(i=0;i<n-1;i++)
  {
    for(j=0;j<2;j++)
     str[index++]=s[i];
  }
  n--;
  for(j=index-1;j>=0;j--)
    str[index++]=str[j];
  str[index]='\0';
  puts(str);
  cnt--;
 }

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

the description is kind of wield.Like we only need 2 record the order the char appears???? abcd goes 2 aabbccccbbaa...how about aba? thats aaaa? what does the 2nd and 3rd iteration mean here ?

- godricly.li October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'''public class subStringWithDuplicate {

public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "ABCD";
for(int i=0; i<str.length(); i++)
{
String disp = "";
for(int j=0; j<str.length()-i-1; j++)
{
disp = disp + str.charAt(j) + str.charAt(j);
}
for(int j=disp.length()-1; j>=0; j--)
{
disp = disp + disp.charAt(j);
}
System.out.println(disp);
}
}

}\\\

- Azad Naik October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// input ABCD
// output AABBCCCCBBAA

 1) start reading from start to (lenght-1) and from Lenngth-1 to Start
 
public string GetNewString(string str)
{
	if(str == null) return str;
	if(str == "") return str;

	StringBuilder sb1= new StringBuilder();
	StringBuilder sb2= new StringBuilder();
	
	for(int i=0,j=str.length-1;i<str.Lenght-1,j>=0;j--,i++)
	{
		sb1.Append(str[i]);
		sb2.Append(str[j]);
	}

	return sb1.ToString() + sb2.ToString();

}

- James November 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below code gives desired output but not a very efficient one

public class RepRev {

    public static void main(String[] args) {

        String str = "ABCD";

        char[] chars = str.toCharArray();

        int length = chars.length;

        while (length > 1) {

            char[] newstr = new char[4 * length - 4];
            int lastindex = newstr.length - 1;

            for (int i = 0; i < length - 1; i++) {

                newstr[2 * i] = chars[i];
                newstr[2 * i + 1] = chars[i];
                newstr[lastindex - 2 * i] = chars[i];
                newstr[lastindex - 2 * i - 1] = chars[i];

            }

            System.out.println(newstr);

            length--;


        }

    }


}

Output:

AABBCCCCBBAA
AABBBBAA
AAAA

- Anonymous January 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution using perl

my $unique_str = "ABCD";

while(length $unique_str){
	$result = &form_string($unique_str);
	print ("\n $result");
	chop($unique_str);
}


sub form_string(){
	$str = $_[0];
	$temp_str = "";
	@letters = split('',$str);
	
	foreach ($i=0; $i < scalar(@letters)-1; $i++){
		$temp_str .= $letters[$i].$letters[$i]; 
		}
		
	$result = $temp_str.reverse($temp_str);
	return $result;
	
}

- saha February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void duplicate(){
		String str = "ABCD";
		StringBuffer buffer = new StringBuffer();
		int index = 1;
		while(index < str.length()){
		for(int i = 0 ; i < str.length()-index; i++) {	
				buffer.append(str.charAt(i));
				buffer.append(str.charAt(i));
			}
			System.out.println(buffer.toString() + buffer.reverse().toString());
			index++;
			buffer = new StringBuffer();
		}
	}

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

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int main()
{
char str[10],*res;
scanf("%s",str);

int i=0,res_id=0,result;
while(str[i]!='\0')
i++;
int N=i;
i=0;
result=4*(N-1) +1;
printf("%d",result);
res=(char *)malloc(sizeof(char)*result);
while(str[i]!='\0'&&str[i+1]!='\0')
{
*(res+res_id)=*(str+i);
*(res+res_id+1)=*(str+i);
res_id=res_id+2;
i++;
// printf("%c",str[i]);
}
i=res_id;
int j=res_id-1;
while(j>=0)
{
*(res+i)=*(res+j);
i++;
j--;

}
*(res+i)='\0';
printf("%s",res);
getch();
return 0;
}

- Anonymous July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

the first and second solutions given above will give the output AABBCCCCBBAA for the input ABCD . What exactly is asked in the question is to go itteration by itteration . what is the expected output ? Is it AAAA or AABBCCCCBBAA ?

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

My solution above gives AAAA which is what I believe is asked.

1 AABBCCDDDDCCBBAA
2 AABBCCCCDDAA
3 AABBBBAA
4 AAAA

- chauvd October 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