Yahoo Interview Question for Software Engineer / Developers






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

Java:

public static void main(String[] args) {
	for (int i=1; i<7; i++) {
		for(int j=0; j<i; j++){
			System.out.print("*");
		}
		System.out.println();
	}
}

- AG November 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static void main(String[] args) {
String stars="";
for (int i=1; i<=5; i++) {
for(int j=0;j<i; j++){
stars = stars + "*";
}
System.out.println(stars);
System.out.println(stars);
stars="";
}
}
Mabbe novice..

- Fix to above November 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i don't think u need so many loops to do that

- Erik April 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

agree..very bad code..one which high school kids write

- sushbis August 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printpattern(int num) {
       char *stars = "*";
       int i = 0;
       for(i=0;i<num;++i) {
          printf("%s\n%s\n", stars, stars);
          strcat(stars,"*");
       }
       return;
}

- Erik April 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the stars pointer should be dynamically allocated, otherwise, "*" cannot be appended to it.

- kevin April 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code just looks shorter, internally strcat iterates through the string to append a "*". In any case the algorithm is no better than O(n2)

- Anonymous May 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Erick's solution is missing one thing. The output expects the star display to be repeated for each i, right? so we need to add one more printf in your for loop. Other than that its fine and yes the algorithm is O(n^2) as strcat has to iterate the full string just to put a "*" at the end of it.

- LLOLer August 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yeah i agree, i want to just give an idea.For doing this simpler.
We can print recursively without using loops.

- Erik April 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

heres another solution:

void printStars(int n) {
char stars[n+1];
for(int j = 0;j < n; j++) {
stars[j] = '*';
stars[j+1] = '\0';
cout<<"\n"<<stars<<"\n"<<stars;
}
}

- goutham June 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printStarList( int n)
    for(int i=0;i<n;i++) {
     if ( (i % 2)==0 ) {
       printNStats(i/2);
      }
     else if ( (i%2) ==1 ) {
       printNStats(i/2+1);
    }  
    }

- Yan September 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printpattern(int num)
{
char *stars = "*";
char *starsnew;
int i = 0;
for(i=0;i<num;++i)
{
printf("%s\n%s\n", stars, stars);
starsnew = (char*)malloc(sizeof(*stars) + 1);
strcat(starsnew , stars);
strcat(starsnew,"*");
stars =starsnew;
}
return ;
}

- devilankur18 November 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's Python:

def printpattern(n):
    [print(2*(i*'*' + '\n'), end='') for i in range(1,n+1)]
printpattern(5)

- Bullocks December 29, 2009 | 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