McAfee Interview Question for Software Engineer / Developers






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

Here is my version of code, Please suggest if anyone has a better approach -

public 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());
}

- kapilraju February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Simple and nice approach. Good one kapil.

- kddev1109 May 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

so nice.

- aaron liang September 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here 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;

}

- Shreyas February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your return string needs to be 2*n! + 1

- Nathan February 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Nathan February 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

He doesn't listen - too busy typing out code to think about the solution.

- Anonymous February 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Nathan,

If i understood you correctly then you mean to say that i should have a null check and intialize the StringBuffer with size 2 * E(n) + 1.

Thanks for your reply.

Kapil

- kapilraju February 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- SRB February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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--;
			}
		}
	}

- abee February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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--;
}

- PKT February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is good
but there shud be i--, before while loop and it shud be while(i>=0) isn't it?

- Sathya February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anyone post a complete working solutioon with no mistakes

- dum August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void construct_pattern(string& input, string& out)
{

string t;

for(int i=0; i<input.length(); i++)
{
for (int j=0; j<=i; j++)
{
out.push_back(input.at(i));
}
}

for(string::reverse_iterator it=out.rbegin(); it!= out.rend(); it++)
t.push_back(*it);

out = out + t;
}

- Sathya August 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
            }



        }

- Enam ul haque September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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);

- Subhash October 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void pattern()
{
      int j,k=1;
      char  *c="abc";

      while(*c)
      {   
                 for(j=0;j<k;j++)
                 cout<<*c;
                 *c++;
                 k++;
      }
      *c--;
      k--;
      while(*c)
      {   
                 for(j=0;j<k;j++)
                 cout<<*c;
                 *c--;
                 k--;
      }
      }

- Anonymous August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *str = "ABCD";
int nlen = strlen (str);
int j = 1, k = 0;
for(int i = 0; i < (nlen*2); i++)
{
j = 1;
if (i < nlen)
{
k++;
while (j <= k)
{
cout<<*(str+i);
j++;
}
}
else
{
while (j <= k)
{
cout<<*(str+k-1);
j++;
}
k--;
}
}

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

char *str = "ABCD";
	int nlen = strlen (str);
	int j = 1, k = 0;
	for(int i = 0; i < (nlen*2); i++)
	{
		j = 1;
		if (i < nlen)
		{
			k++;
			while (j <= k)
			{
				cout<<*(str+i);
				j++;
			}
			cout<<"-";
		}
		else
		{
			while (j <= k)
			{
				cout<<*(str+k-1);
				j++;
			}
			cout<<"-";
			k--;
		}
	}

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

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

- Praveen D February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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]

- Anubhav March 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Praveen have u teated it

- deepak February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void repetetivePalindrome(String x){
		char in [] = x.toCharArray();
		StringBuilder sb = new StringBuilder();
		for(int i=0 ; i<in.length; i++){
			for(int j=0;j<=i; j++){
				sb.append(in[i]);
			}
		}
		
		String result  = sb.toString() + sb.reverse().toString();
		System.out.println(result);

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

int main()
{
	string s = "abc";
	string s1, resStr;
	int i, j, k = 0 ;
	bool done = false;	
	for( i= 0; i < s.length(); ++i)
	{
		for( j = 0; j < i+1; ++j )
		{
			s1 += s[i];
		}
				
	}
	resStr += s1;
	resStr.append(strrev( const_cast<char*>( s1.c_str() )) );

	cout << resStr <<endl;	
	P;
	return 0;
}

- Anitesh March 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}
}

- Manish March 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why did you choose a stack version??

- dum March 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

}
}

- sunil Balakrishnan October 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A small correction in second while loop condition is j<=i . , pls consider this while reading

- sunil balakrishnan October 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cstring>
 using namespace std;
 
int main()
 {
 	char a[10];
 	int j;
 	int i;
 	int flag=0;
 	
 	cout<<"Enter the input\n";
 	gets(a);
 	
 	for(i=0; a[i]!='\0'; ++i)
 	{
 	  j=0;
 	 
 	 while(j<=i)
 	 {
 	 	cout<<a[i];
 	 	
 	 	j++; 	 	
 	 
			  }
		}
			  
		for(;i>=0;--i)
		{
			j=0;
			
			while(j<=i)
			{
				cout<<a[i];
				j++;
			}
		}
		  }

- Diwakar June 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <bits/stdc++.h>

using namespace std;

int main() {
string str;
cin>>str;
string newSt="";
int l=str.length();
for(int i=0;i<l;i++)
{
newSt+=string(i+1,str[i]);
}
string rev=newSt;
reverse(rev.begin(),rev.end());
newSt+=rev;
cout<<newSt;
return 0;
}

- Anonymous August 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <bits/stdc++.h>

using namespace std;

int main() {
string str;
cin>>str;
string newSt="";
int l=str.length();
for(int i=0;i<l;i++)
{
newSt+=string(i+1,str[i]);
}
string rev=newSt;
reverse(rev.begin(),rev.end());
newSt+=rev;
cout<<newSt;
return 0;
}

- Riddhi August 29, 2017 | 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