Microsoft Interview Question
Testing / Quality AssurancesCountry: India
Interview Type: In-Person
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.
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]);
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
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;
}
}
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
#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;
}
#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);
}
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");
}
}
}
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");
}
}
}
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;
}
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;
}
}
}
'''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);
}
}
}\\\
// 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();
}
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
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;
}
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();
}
}
#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;
}
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 ?
C:
- S O U N D W A V E October 03, 2013