Pyramid Printing

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 3

import java.util.

*;

class PyramidPrinting {
        public static void main(String args[]) {
                Scanner sc = new Scanner(System.in);
                int n = sc.nextInt();

                // System.out.println("Stars:" + getStars(1));
                // System.out.println("Stars:" + getStars(2));

                String[] pattern = pyramidPrinting(n);

                for (int i = 0; i < n; i++)


                        System.out.println(pattern[i]);

    }

        // eg: i=1,
        // *
        // eg: i = 2,
        // * *
        public static String getStars(int i) {
                StringBuilder starBuilder = new StringBuilder();
                if (i == 0) {
                        System.out.println("This is probably passed by error");
        }

                for (int j=0; j < i ; j++) {


                        if (j != 0) {
                                starBuilder.append(" ");     
            }
                        starBuilder.append("*");
        }

                // System.out.println(starBuilder.toString().length());
                return starBuilder.toString();
    }

        static String[] pyramidPrinting(int n) {


                String[] pyramidStars = new String[n];

                // for i : 1 to 4
                for (int i = 0; i < n ; i++) {
                        // eg: i=1,
                        // *
                        // eg: i = 2,
                        // * *
                        pyramidStars[i] = getStars(i + 1);
                } // for i.
                //          for each i, getStars(i)
                //          the return string gets appended to the array
                // return the string array

                // TODO: Make sure that there are no trailing space after the last star
                return pyramidStars;
    }
}

// Milestone 1: Understand the problem clearly


// - Ask questions & clarify the problem statement clearly.
// - Take an example or two to confirm your understanding of the input/output & extend it to
testcases
// 4
// *
// * *
// * * *
// * * * *

// 1
// *

// Milestone 2: Finalize approach & execution plan


// - Understand what type of problem you are solving.
// - Brainstorm multiple ways to solve the problem and pick one
// eg: for 1 - 4
// for each row, going to get the number of stars
// // - Get to a point where you can explain your approach to a 10 year old - Done
// // - Take a stab at the high level logic & write it down.
// for i : 1 to 4
//          for each i, getStars(i)
//          the return string gets appended to the array
// return the string array

// Make sure that there are no trailing space after the last star

// - Try to offload processing to functions & keeping your main code small.

// Milestone 3: Code by expanding your pseudocode


// - Make sure you name the variables, functions clearly.
// - Avoid constants in your code unless necessary; go for generic functions, you can use
examples for your thinking though.
// - Use libraries as much as possible

// Milestone 4: Prove to the interviewer that your code works with unit tests
// - Make sure you check boundary conditions

// Time & storage complexity


// Suggest optimizations

You might also like