McAfee Interview Question
Software Engineer / DevelopersHere is the code:
char *pattern_convertion(char *str)
{
char *ret = NULL;
int l = strlen(str);
if(l == 0) return NULL;
if((ret = (char *)malloc(l*sizeof(char))) == NULL) return NULL;
int c = 0, i,j;
for(i = 0; i<l; i++)
{
for(j = 0; j<i+1; j++, c++)
ret[c] = str[i];
}
for(i = l-1; i>=0; i--)
{
for(j = 0; j<=l; j--, c++)
ret[c] = str[i];
}
str[c] = '\0';
return str;
}
Sorry, i mean the string buffer size needs to be 2 * E(n) + 1
ie, abc = 1 a, 2 bs, 3cs, ie E(3) = 3 + 2 + 1, doubled for the reverse, and a null terminator.
char *pattern_convertion(char *str)
{
char *ret = NULL;
int l = strlen(str);
if(l == 0) return NULL;
if((ret = (char *)malloc(l*sizeof(char))) == NULL) return NULL;
int c = 0, i,j;
for(i = 0; i<l; i++)
{
for(j = 0; j<i+1; j++, c++)
ret[c] = str[i];
}
for(i = l-1; i>=0; i--)
{
for(j = 0; j<=l; j--, c++)
ret[c] = str[i];
}
ret[c] = '\0';
return ret;
}
is it better to use char array? m converting string to char array and then working with it.
public static void getOutput(String str)
{
System.out.println("\n Input="+str);
char[] c=str.toCharArray();
int k=0;
int i;
for (i=0; i<c.length ; i++)
{
k=0;
while(k<=i)
{
System.out.print(c[i]);
k++;
}
}
while(k>0)
{
k--;
i=k;
while(i>=0)
{
System.out.print(c[k]);
i--;
}
}
}
Algo:
1.take a loop to traverse string from zero to string length.
2.one more loop inside first loop which will responsible for no. of times a particular char to be printed.
3.same concept with little variation to print remaining half string.
------------------------------
for(i=0;i<length;i++)
{
for(j=0;j<=i;j++)
{
system.out.println(str.charAt(i));
}
}
while(i!=0)
{
for(j=0;j<=i;j++)
{
system.out.println(str.charAt(i));
}
i--;
}
Here is my recursive solution using C#. Guys please discuss the complexities
//call using : print("abc", "", 0, 0, 0, 12);
void print(string input, string output, int cp, int op, int closeCount, int total)
{
if (closeCount >= 12)
{
Console.WriteLine(output);
return;
}
if (closeCount < total / 2)
{
if (cp == op)
{
cp = cp + 1;
op = 0;
}
print(input, output + input[cp - 1], cp, op + 1, closeCount + 1, total);
}
else
{
if (cp > 0 && cp == op )
{
cp = cp - 1;
op = -1;
}
print(input, output + input[cp], cp, op + 1, closeCount + 1, total);
}
}
String str="abc";
String res ="";
char[] s=str.toCharArray();
// abbccccccbba
int count=0;
int tmp=0;
int pp=0;
int len=str.length();
while(count<len)
{ tmp=count;
final int su=tmp;
while(tmp>=0)
{
res+=s[su];
tmp--;
}
count++;
}
while(len-1>=0)
{ tmp=len-1;
final int su=tmp;
while(tmp>=0)
{
res+=s[su];
tmp--;
}
len--;
}
System.out.println(res);
public static void main(String[] args)
{
char[] chracters = args[0].toCharArray();
StringBuffer buffer = new StringBuffer();
for(int i=0, j=0; i<chracters.length; i++)
{
while(j <= i)
{
buffer.append(chracters[i]);
j++;
}
j=0;
}
System.out.println(buffer.toString());
System.out.println(buffer.toString() + buffer.reverse());
}//End of Main
Here is my solutio:
[code]
void pattern(char str[])
{
int length = strlen(str);
if(length == 0)
return;
int tlength= (sizeof(char) * length*length) + 1;
char *s = (char*)malloc(tlength);
s[tlength] = '\0';
int memcount = 0;
for(int i =1;i<=length;i++)
{
int cnt =1;
while(cnt <= i)
{
s[memcount] = str[i-1];
s[tlength -1 -memcount] = str[i-1];
memcount++;
cnt++;
}
}
cout<<s;
}
int main()
{
char n[50];
cout<<"Enter the string:";
cin>>n;
pattern(n);
getch();
return 0;
}
[/code]
package abc;
import java.util.Stack;
public class ABCSpecial {
public static void main(String[] args){
MyStack stack = new MyStack();
char[] myCharString = args[0].toCharArray();
int length = 0;
while(length!=myCharString.length){
stack.push(myCharString[length]);
length++;
}
while(length!=0){
stack.pop();
length--;
}
}
}
class MyStack extends Stack<Character>{
static int i = 1;
@Override
public Character push(Character obj){
Character myChar = super.push(obj);
for(int y=0;y<i;y++){
System.out.print(myChar);
}
i++;
return myChar;
}
@Override
public Character pop(){
Character myChar = super.pop();
for(int y=1;y<i;y++){
System.out.print(myChar);
}
i--;
return myChar;
}
}
Here we can do it in a very simple way, because there is a simple recurring pattern .
ie, first letter 1 time second 2 time third 3 time... nth n time , nth n time, n-1 th n-1 time,..
3rd 3 time 2 nd 2 time 1 st 1 time .
so the code snip will be like this,
int main ()
{
char str[] = " abcd";
int l = strlen(str);
int j;
for ( int i =0; i<l; i++)
{
j=0;
while (j<=i) { cout << str[i]; j++}
}
for ( int i=l-1;i>=0;i--)
{
j=0;
while(j>=i)
{ cout<<str[i]; j++;}
}
this has the time complexity O(n)
}
}
Here is my version of code, Please suggest if anyone has a better approach -
- kapilraju February 13, 2011public static void main(String[] args) {
String input = "abc";
int length = input.length();
StringBuilder str = new StringBuilder();
for (int i = 0; i < length; i++) {
for (int j = 0; j < i+1; j++) {
str.append(input.charAt(i));
}
}
System.out.println(str.toString()+str.reverse().toString());
}