Accenture Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

I would construct the triangle bottom - up. Memorize it into a ArrayList and when you want to print it go from the end of the ArrayList to the beginning.

You can see that each row starts with k stars ( the last row has 0, the n-1 has 1, the n-2 has 2 and so on).

You also can see that inside a row , between 2 numbers there is only one star.

Use this and you have your solution.

- Kamy December 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud job

- sunny nanda February 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 5 vote

Here is the Java code:

public class PrintTriangle {
public static void main(String[] args) {
int i,j,k,l = 0,n;
n = 10;
for( i = 1; i<= n; i++){
k = n - (i - 1);
l = 1;
for ( j = 1; j <= 2 * n - 1; j++){
if (l <= i && j == k){
System.out.print(i + "\t");
k = k + 2;
l++ ;
}
else
System.out.print("*\t");
}
System.out.println();
}

}
}

- havefun December 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why this is voted -ve? Please give the reason before giving it a vote. It actually produces the output.

- Anonymous December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is much like triangle.
0.1) Assume: Line Count (n) is given
0.2) Assume: n <= 32
0.3) Print Row - Prints a '*' when '0', print input number when '1'
1) Item Count per line would be 2 * n -1
2) Have two long integers, say currRow, prevRow
2.1) Set prevRow = 0, currRow = 1 << n
2.2) Print currRow with '1'
3) for integer k from 2 to n (inclusive on both sides)
3.1) prevRow = currRow and currRow = 0
3.2) For every bit '1' in prevRow (say at index 'm'), set bit 'm-1' and 'm+1' to 1 in currRow
3.3) Print currRow with 'k'

Step (3.2) - Is actually a loop (though a single statement in text)

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

public class Triangle {
	
	public static void main(String arg[]){
		int height = 9;
		
		for(int i=0;i<height;i++ ){
			for(int j=height-1;j>i;j--){
				System.out.print("*");
			}
			
			boolean printChar = false;
			for(int j=0;j<((i*2)+1);j++){
				if( printChar ){
					System.out.print("*");					
				}
				else{
					System.out.print(i+1);
				}
				printChar = !printChar;
			}
			
			for(int j=height-1;j>i;j--){
				System.out.print("*");
			}
			
			System.out.println();
		}
	}




========================
output:
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
**7*7*7*7*7*7*7**
*8*8*8*8*8*8*8*8*
9*9*9*9*9*9*9*9*9

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

class print2
{
public static void main(String s[])
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=3;j>=i;j--)
{
System.out.print("*");
}
for(j=1;j<=2*i-1;j++)
{
if(j%2==0)
System.out.print("*");
else
System.out.print(i);

}
for(j=3;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}

- meenakshi January 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class TestString {
    public static void main(String[] args) {
    	int adder=3,printer=0;
       		for(int i =1 ;i<=4;i++)
       		{
       			for(int j=1;j<=adder;j++)
       			{
       				System.out.print("*");
       			}
       			for(int k=1;k<=i;k++)
       			{
       				if(i==4 && k==4)
       					{
       					System.out.print(i);
       					}
       				else{
       					System.out.print(i+"*");
       				}
       			}
       			for(int j=1;j<adder;j++)
       			{
       				System.out.print("*");
       			}  				
       			adder--;
       			System.out.println();
       		}
    }
}

- Saurabh Anand January 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.test.me;

public class PrintPattern {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int length = 30;
int nolines = 30;
String[][] tmpar= new String[nolines][length];
for(int j=0;j<nolines;j++){
for(int i=0;i<length;i++){
tmpar[j][i] = "*";
}

}
int k =1;
int p =0;
int z =0;
for(int j=0;j<nolines;j++){
p = k/2;
z = k/2;
if(k%2 !=0){
tmpar[j][length/2] = String.valueOf(k);
if(k != 1){
for(int i=length/2+2;i<length;i++){
tmpar[j][i] = String.valueOf(k);
i++;
p = p-1;
if(p==0){
break;
}
}
for(int i=length/2-2;i>=0;i--){
tmpar[j][i] = String.valueOf(k);
i--;
z = z-1;
if(z==0){
break;
}
}
}
}else{
for(int i=length/2+1;i<length;i++){
tmpar[j][i] = String.valueOf(k);
i++;
p = p-1;
if(p==0){
break;
}
}
for(int i=length/2-1;i>=0;i--){
tmpar[j][i] = String.valueOf(k);
i--;
z = z-1;
if(z==0){
break;
}
}
}
k++;


}
for(int j=0;j<nolines;j++){
for(int i=0;i<length;i++){
System.out.print(tmpar[j][i]);
}
System.out.print("\n");
}



}

}

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

Is it like this

public class Latihan1 {
    private int totalStar;
    private int edgeStar;
    private int centerStar;
    private int totalCenter;
    private int totalPrint;
    public Latihan1() {
        totalStar = edgeStar = centerStar = 0;
    }
    public static void main(String[] args) {
        new Latihan1().run();
    }
    public void run() {
        for(int i = 1; i <= 4; ++i) {
            totalStar = 7 - i;
            if(i == 1) {
                edgeStar = totalStar/2;
                centerStar = 0;
            }
            else {
                edgeStar = totalStar/i;
                if(totalStar > i)
                    centerStar = totalStar % i;
                else
                    centerStar = 1;
           }
           if(i == 1)
	           totalCenter = 0;
           else
               totalCenter = (totalStar - (2*edgeStar))/centerStar;
            totalPrint = totalCenter + 2 + i;
            for(int k = 0; k < totalPrint; ++k) {
                if(k == 0 || k == totalPrint-1) {
                    printEdgeStar(edgeStar);
                }
                else if(k > 0 && k % 2 == 1) {
                    printChar(i);
                }
                else
                    printCenterStar(centerStar);
            }
            System.out.println();
        }
    }
    
    public void printEdgeStar(int totalStar) {
        for(int j = 0; j < totalStar; ++j)
            System.out.print("*");
    }
    
    public void printCenterStar(int totalStar) {
        for(int j = 0; j < totalStar; ++j)
            System.out.print("*");
    }
    
    public void printChar(int x) {
        System.out.print(x);
    }
}

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

class Demo
{

public static void main(String args[])
{
int n=6,y=1,x=3,v=-1,p=-1,q=-1,r=-1,s=-1,count=0,d=0;
for (int i=0;i<4 ;i++ )
{
for(int j=0;j<n;j++)
{


if(j==x||j==p||j==q||j==r||j==s)
{
System.out.print(y);
//continue;
}

System.out.print("*");

}
n--;
count++;
//System.out.print(y);
p=x;x--;y++;
if(count==2)
{
q=3;
}
else if(count==3)
{
r=2;
s=3;
n=n+1;
}

System.out.println();

}

}

}

- yogesh March 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void printFunny(int n) {
		
		for (int i =1; i <= n; i++) {
			
			for (int j=0; j < n-i; j++) {
				System.out.print("*");
			}
			for (int k = 0; k < 2*(i-1) + 1; k++) {
				if (k % 2 == 0)
					System.out.print(i);
				else
					System.out.print("*");
			}
			for (int l=0; l < n-i; l++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		System.out.println();
		
	}

Output results:
***1***
**2*2**
*3*3*3*
4*4*4*4

****1****
***2*2***
**3*3*3**
*4*4*4*4*
5*5*5*5*5

******1******
*****2*2*****
****3*3*3****
***4*4*4*4***
**5*5*5*5*5**
*6*6*6*6*6*6*
7*7*7*7*7*7*7

********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
**7*7*7*7*7*7*7**
*8*8*8*8*8*8*8*8*
9*9*9*9*9*9*9*9*9

- init.d November 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class test {

	private static int length;

	
	private static int getRange(int numb,boolean sign){
		if(sign)
	     return (length/2)+(numb-1);
		else
			 return (length/2)-(numb-1);
		
	}
	
	
	
	private static void getRow(int numb){
		
		int low=getRange(numb,false);
		int high=getRange(numb,true);
		
		for(int k=0;k<length;k++){
			
			if(k==low){
				
				int j;
				
		         for(j=k;j<high+1;j=j+2){
		        	 
		        	 
		        	 System.out.print(numb+"*");        	 
		        	 
		         }
		         
		        k=j-1;
				
			  }else{
			
			System.out.print("*");
			  }
		}
		
	}
	
	
	private static void getTree(int numb){
		
		length=(numb*2)+1;
		
		for(int i=1 ; i<numb+1;i++){	
			getRow(i);
			System.out.println();
		}
		
		
	}
	
	
	
	public static void main(String []args){
			
		getTree(4);
		
	
	}
	
}

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

i want the code of
*1
**2
***3

- dheeraj kumar March 26, 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