Interview Question


Country: United States




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

#include<stdio.h>
main()
{
 	int i,j,k,h,p;
 	printf("Enter height of the triangle : ");
 	scanf("%d",&h);                  
 	for(i=0;i<h;i++)                        
 	{
 		for(k=i;k<h;k++)                    
 		printf(" "); 
 		for(j=0;j<=i;j++)                   
 		{
 			p = pascal(i,j);                            
 			printf("%3d",p);                            
		 }
		 printf("\n");
	 }
}
 
int pascal(int r,int c)                     
{
	 if(c==0 || r==c)
		 return 1;
	 else
		 return pascal(r-1,c-1)+pascal(r-1,c);       
}

- gAumz March 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I like your solution, it doesn't require explicit extra-memory, however pascal method does the whole re-processing for every new row, having an array of previous values would dramatically improve performance. However this is the requirement.

- Marcello Ghali March 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Same implementation but with more meaningful variable names for people who haven't seen it before.

void DrawPascalTriangle(int maxRows)
{
	int columnValue;

	// Loop rows
	for (int currentRow = 0; currentRow < maxRows; currentRow++)
	{
		// Create row, have height number of entries per row
		for (int currentColumn = 0; currentColumn <= currentRow; currentColumn++)
		{
			columnValue = Pascal(currentRow, currentColumn);
			printf("%3d", columnValue);
		}

		printf("\n");
	}
}

int Pascal(int currentRow, int currentColumn)
{
	if (currentColumn == 0 || currentRow == currentColumn)
		return 1;
	else
		return Pascal(currentRow - 1, currentColumn - 1) + Pascal(currentRow - 1, currentColumn);
}

- CareerCupDom March 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main()
{
int i,j,k,h,p;
printf("Enter height : ");
scanf("%d",&h);
for(i=0;i<h;i++)
{
for(k=i;k<h;k++)
printf(" ");
for(j=0;j<=i;j++)
{
p = pascal(i,j);
printf("%3d",p);
}
printf("\n");
}
}

int pascal(int r,int c)
{
if(c==0 || r==c)
return 1;
else
return pascal(r-1,c-1)+pascal(r-1,c);
}

- gAumz March 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String printCrazy(int n){
		
		if(n == 1){
			System.out.println("1");
			return "1";
		}

		if(n > 1){
			
			String returnPrev = printCrazy(n-1);
			String [] parts = returnPrev.split(" ");
			StringBuffer returnCur = new StringBuffer();
			for(int i=0;i<parts.length;i++){	
				if(i==0){
					returnCur.append(parts[0]);
					returnCur.append(" ");
				}

				
				if(i > 0 && i < parts.length){
				
					int calculatedValue = Integer.parseInt(parts[i-1]) + Integer.parseInt(parts[i]);
					returnCur.append(calculatedValue);
					returnCur.append(" ");
				}
				
				if(i == parts.length-1){
					returnCur.append(parts[parts.length-1]);
					returnCur.append(" ");
				}
						
			}
			
			System.out.println(returnCur.toString().trim());
			return returnCur.toString().trim();
		}else
			return "";
	}

- akella.mahesh March 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
        pascalTriangle(0, 5, null);
    }

    private static void pascalTriangle(int start, int end, int [] old) {
        if(start == end + 1)
            return;
        
        int [] arr = new int[start + 1];
        arr[0] = 1;
        arr[arr.length - 1] = 1;
        
        for(int i = 1; i < arr.length - 1; i++)
            arr[i] = old[i - 1] + old[i];
        
        for(int i : arr)
            System.out.print(i + " ");
        System.out.println("");
        
        pascalTriangle(start + 1, end, arr);
    }


1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1

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

using extra memory O(N) N == row number

public void generateAll (int row){
		int [] memory = new int [row];
		memory[0] = 1;
		generate (row, 0, memory) ;
	}
	
	private void generate (int row, int count , int [] memory){
		if (row == count) {			
			System.out.println();
			return  ;
		}
		if (count == 0) {
			System.out.println(memory[0] + " ");
			generate (row, count + 1 , memory);
		} else {
			int pre = 0 ;
			for (int i = 0 ; i <= count ; ++i) {
				int rear = i == count ? 0 : memory[i] ;
				int c = pre + rear ;
				pre = rear ;
				memory[i] = c ;
			}
			for (int i = 0 ; i <= count ;++i) {
				System.out.print(memory[i] + " ");
			}
			System.out.println();
			generate (row, count + 1 , memory);	
		}		
	}

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

C code using recursion to print pascals triangle

#include <stdio.h>
#include <stdlib.h>

int fact(int n){
int j=1,fact=1;
while(j<=n){
fact=fact*j;
j++;
}
return fact;
}

void draw_line(int i,int j){
printf("1 ");
for(;j<i;j++){
printf("%d ",fact(i)/(fact(i-j)*fact(j)));
}
puts("1");
}

void rec_pascal(int i,int n){
if(i==n)
return;
if(i==1){
printf("1\n");
}
draw_line(i,1);
rec_pascal(i+1,n);
}

int main()
{
int n=5;
rec_pascal(1,n);
    return 0;
}

- gowtham kesa March 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Pattern {
    private void generatePattern(int rowCounter, int row) {
        if (rowCounter > row) {
            return;
        }

        int[] arr = new int[rowCounter + 1];
        for (int i = 0; i < rowCounter; i++) {
            arr[i] = getFactorial(rowCounter - 1)
                    / (getFactorial(i) * getFactorial(rowCounter - i - 1));
        }
        display(arr, rowCounter);
        rowCounter++;
        generatePattern(rowCounter, row);
    }

    private void display(int[] arr, int rowCounter) {
        for (int i = 0; i < rowCounter; i++) {
            System.out.print(arr[i] + "\t");
        }
        System.out.println();
    }

    private int getFactorial(int num) {

        if (num <= 1) {
            return 1;
        }
        return num * getFactorial(num - 1);
    }

    public void getPattern(int row) {
        generatePattern(0, row);
    }
}

public class DisplayTrainglePattern {
    public static void main(String[] args) {
        Pattern mPattern = new Pattern();
        mPattern.getPattern(7);

        mPattern.getPattern(9);
    }
}

- Scorpion King April 03, 2015 | 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