Google Interview Question for SDE1s


Country: United States




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

public class Solution {

    public static void main(String [] args) {

        printTree(4);

        System.out.println();
        System.out.println();
        System.out.println();

        printTree(10);

        System.out.println();
        System.out.println();
        System.out.println();

        printTree(20);

        System.out.println();
        System.out.println();
        System.out.println();

        printTree(25);
        printTree(1);

    }

    public static void printTree(int day) {
        if (day  < 2) {
            System.out.println("You cannot generate christmas tree");
            return;
        }

        if (day > 20) {
            System.out.println("Tree is no more");
            return;
        }

        StringBuilder buf = new StringBuilder();

        // first level
        for (int i = 1, j = day; i <= day*2 + 1; i = i+2, j-- ) {
            // space
            for (int a = 0; a <= j; a++) {
                buf.append(' ');
            }

            // star
            for (int a = 0; a < i; a++) {
                buf.append('*');
            }

            buf.append('\n');
        }

        for (int b = 1; b < day-1; b++) {

            for (int i = 3, j = day-1; i <= day*2 + 1 - b*2; i = i+2, j-- ) {
                // space
                for (int a = 0; a <= j; a++) {
                    buf.append(' ');
                }

                // star
                for (int a = 0; a < i; a++) {
                    buf.append('*');
                }

                buf.append('\n');
            }

        }

        for (int j = 0; j < 2; j++) {
            for (int i = 0; i <= day; i++) {
                buf.append(' ');
            }
            buf.append('*').append('\n');
        }


        System.out.println(buf.toString());
    }

}

- ugurdonmez87 July 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the example from your first image and the sample output in the second are the same? What's going on here?

- what July 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Input will be taken from the command line while invoking it*/

class MyChristmasTree {

public static void main(String[] days)
{
	int n = Integer.parseInt(days[0]);
	if(n < 2) 
		System.out.println("You cannot generate christmas tree");
	else if(n > 20)
		System.out.println("Tree is no more");
	else {
	int max_leaves = 3+2*(n-1);
	int dist = max_leaves/2;
	String print = "";
	for(int i=0; i<dist; i++)
		print = print + " ";
	print = print + "*";
	System.out.println(print);
	int leaves = 3;
	for(int i=0; i<n-1; i++)
	{
		for(int j=0; j<(n-i); j++)
		{
			dist = dist - 1;
			print = "";
			for(int k=0; k<dist; k++)
			{
				print = print + " ";
			}
			for(int k=0; k<leaves; k++)
			{
				print = print + "*";
			}
			System.out.println(print);
			leaves = leaves + 2;			
		}
		leaves = 3;
		dist = max_leaves/2;
	}
	dist = max_leaves/2;
	print = "";
	for(int i=0; i<dist; i++)
		print = print + " ";
	print = print + "*";
	System.out.println(print);
	System.out.println(print);
	}

}

}

- justanothercoder August 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<bits/stdc++.h>
using namespace std;
int generate(int n)
{
	
	if(n<2)
	{
	    cout<<"You cannot generate christmas tree"<<endl;
	    return 0;
	}
	else if(n>20)
	{
	    cout<<"Tree is no more"<<endl;
	    return 0;
	}
	int i,j;
	for(i=0;i<=n;i++)
	{
	    for(j=0;j<n-i;j++)
	    cout<<" ";
	    for(j=0;j<=i;j++)
	    cout<<"*";
	    for(j=0;j<i;j++)
	    cout<<"*";
	    cout<<endl;
	}
	if(n>2)
	for(i=1;i<n;i++)
	{
	    for(j=0;j<n-i;j++)
	    cout<<" ";
	    for(j=0;j<=i;j++)
	    cout<<"*";
	    for(j=0;j<i;j++)
	    cout<<"*";
	    cout<<endl;
	}
	if(n>3)
	for(int k=1;k<=n-3;k++)
	for(i=1;i<n-k;i++)
	{
	    for(j=0;j<n-i;j++)
	    cout<<" ";
	    for(j=0;j<=i;j++)
	    cout<<"*";
	    for(j=0;j<i;j++)
	    cout<<"*";
	    cout<<endl;
	}
	for(i=0;i<n;i++)
	cout<<" ";
	cout<<"*"<<endl;
	for(i=0;i<n;i++)
	cout<<" ";
	cout<<"*"<<endl;
	return 0;
}
int main()
{
	int n;
	cin>>n;
	generate(n);
	return 0;}

- s.ragavendraselvam August 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) 
	{
		printTree(3," ");
		
		System.out.println();
		System.out.println();
		System.out.println();
	}
	
	
	public static  void printTree(int day,String offset) 
	{
		String singleSpace=" ";
		String star="*";
		
		// Base cases
		if(day <=1)
		{
			System.out.println("You cannot generate christmas tree");
			return;
		}
		
		if(day>20 )
		{
			System.out.println("Tree is no more");
			return;
		}	
		
		//Printing the top star??? 
		System.out.print(offset);
		System.out.println(getSpecialString(day,singleSpace)+""+getSpecialString(1,star));
		
		//Printing the Parts
		int parts = day-1;
		
		int levelDiff = day*2+1;
		
		for(int i=0;i<parts;i++)
		{ 
			int spaces = day-1;
			for(int j=3;j<=levelDiff;j+=2)
			{
				System.out.print(offset);
				System.out.println(getSpecialString(spaces,singleSpace)+""+getSpecialString(j,star));
				spaces-=1;
			}
			levelDiff-=2;
		}
		//Printing the stand
		System.out.print(offset);
		System.out.println(getSpecialString(day,singleSpace)+""+getSpecialString(1,star));
		System.out.print(offset);
		System.out.println(getSpecialString(day,singleSpace)+""+getSpecialString(1,star));
		
	}
	
	private static String getSpecialString(int n,String character)
	{
		StringBuilder sb =  new StringBuilder();
		for (int i = 0; i < n; i++) {
			sb.append(character);
		}
		return sb.toString();

}

- karldren8 November 12, 2016 | 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