Facebook Interview Question for SDE1s


Country: United States




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

function drawDiamond(n) {
     var reduce = 2;
     var star = 1, space = (n - 1)/2;
     
     for (var i = 0; i < n; i++) {
       console.log(loopStr(space," ") + loopStr(star,"*") + loopStr(space," "));
       star  = star + reduce;
       space = ((n - star)/2);
       if (star == n) reduce = -2;
     }
    }
    
    function loopStr(n,str) {
        var result = "";
        for (var i = 0; i < n; i++) {
            result += str;
        }
        return result;
    }
    
    console.log(drawDiamond(7))

- Hien Luong May 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class PrintDiamondShape {

public static void main(String[] args) {
// TODO Auto-generated method stub

int num =9;
int l=1;
for (int i = 0; i <=num/2; i++) {
for (int j = i; j < num/2; j++) {
System.out.print(" ");
}
for (int k = 0; k <l; k++) {
System.out.print("*");
}
for (int j = i; j < num/2; j++) {
System.out.print(" ");
}
System.out.println();
l= l+2;
}
int x=0;
for (int i = 0; i <num/2; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
for (int k = 0; k <num-2-x; k++) {
System.out.print("*");
}
x=x+2;
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.println();

}

}

}

- Vipul Agarwal May 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

void printUp(int n, int max, int space) {
	if (n >= max) return;

	for (int i = 0; i < space - 1; ++i) {
		printf(" ");
	}

	for (int i = 0; i < n; ++i) {
		printf("*");
	}
	printf("\n");

	printUp(n + 2, max, space - 1);
}


void printDown(int n, int space) {
	if (n <= 0) return;

	for (int i = 0; i < space; ++i) {
		printf(" ");
	}	

	for (int i = 0; i < n; ++i) {
		printf("*");
	}

	printf("\n");

	printDown(n - 2, space + 1);
}

void print(int n) {
	printUp(1, n, (n / 2) + 1);
	printDown(n, 0);
}

int main(int argc, char **argv) {
	print(5);
	return 0;

}

- paul.boutes May 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Objective-C...

- (void) printDiamond:(int)n
{
    if (n <= 0 || (n % 2 != 1)) {
        NSLog(@"Bad Input");
        return;
    }
    [self printDiamondLine:n spaceCount:(n/2)];
}

- (void) printDiamondLine:(int)n spaceCount:(int)spaceCount
{
    if (spaceCount == 0) {
        NSLog(@"%@", [self getStringOfK:n ch:@"*"]);
    }
    else {
        NSLog(@"%@%@", [self getStringOfK:spaceCount ch:@" "], [self getStringOfK:(n - 2 * spaceCount) ch:@"*"]);
        [self printDiamondLine:n spaceCount:(spaceCount - 1)];
        NSLog(@"%@%@", [self getStringOfK:spaceCount ch:@" "], [self getStringOfK:(n - 2 * spaceCount) ch:@"*"]);
    }
}

- (NSString *) getStringOfK:(int)k ch:(NSString *)ch
{
    NSString *output = @"";
    for (int i = 0; i < k; i++) {
        output = [output stringByAppendingString:ch];
    }
    return output;
}

- Pmatt May 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main()
{
int n, c, k, space = 1;

printf("Enter number of rows\n");
scanf("%d", &n);

space = n - 1;

for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");

space--;

for (c = 1; c <= 2*k-1; c++)
printf("*");

printf("\n");
}

space = 1;

for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");

space++;

for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*");

printf("\n");
}

return 0;
}

- Anonymous May 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
 
int main()
{
  int n, c, k, space = 1;
 
  printf("Enter number of rows\n");
  scanf("%d", &n);
 
  space = n - 1;
 
  for (k = 1; k <= n; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");
 
    space--;
 
    for (c = 1; c <= 2*k-1; c++)
      printf("*");
 
    printf("\n");
  }
 
  space = 1;
 
  for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");
 
    space++;
 
    for (c = 1 ; c <= 2*(n-k)-1; c++)
      printf("*");
 
    printf("\n");
  }
 
  return 0;
}

- Anonymous May 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//O(n) time
public void print(int n){
	if(n <= 0 || n % 2 != 0){
		throw new IllegalArgumentException();
	}
	for(int i = 1; i < n; i += 2){
		printStars(i);
		System.out.print('\n');
	}
	printStars(n);
	for(int i = n - 2; i >= 1; i -= 2){
		System.out.print('\n');
		printStars(i);
	}
}

private void printStars(int n){
	while(n > 0){
		System.out.print('*');
		n--;
	}
}

- divm01986 May 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

def printPattern(n):
    i = 1
    while(True):
        print (n-i)/2 * " " + i * "*"
        if i >= n:
            break
        i += 2
    i=n-2
    while(True):
        print (n-i)/2 * " " + i * "*"
        if i <= 1:
            break
        i -= 2

- ankitpatel2100 May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;

// To execute C#, please define "static void Main" on a class
// named Solution.

class Solution
{

static void PrintDiamond(int n)
{
int count = n/2;
for(int i=0;i<n*2;i++)
{
if(i%2!=0)
Console.WriteLine("{0}{1}",new String(' ',i<n ? count-- :count++),new String('*', i<n ? i :n*2 - i ));

}
}
static void Main(string[] args)
{
PrintDiamond(399);

}
}

- Suchit May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
class Solution
{
    
    static void PrintDiamond(int n)
    {
        int count = n/2;
        for(int i=0;i<n*2;i++)
        {
            if(i%2!=0)
                Console.WriteLine("{0}{1}",new String(' ',i<n ? count-- :count++),new String('*', i<n ? i :n*2 - i ));
                
        }
    }
    static void Main(string[] args)
    {
        PrintDiamond(7);
      
    }
}

- Anonymous May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void PrintStarPattern(int N)
        {
            PrintPatternInternal(1, N, 2);
            PrintPatternInternal(N-2, N, -2);
        }

        private static void PrintPatternInternal(int k, int N, int delta)
        {
            if (k < 1 || k > N)
            {
                return;
            }

            PrintLine(k, N);
            PrintPatternInternal(k + delta, N, delta);
        }

        private static void Print(int n, char c)
        {
            if (n < 0)
            {
                throw new ArgumentException();
            }

            for (int i = 1; i <= n; i++)
            {
                Console.Write(c);
            }
        }

        private static void PrintLine(int k, int N)
        {
            Print((N - k)/2, ' ');
            Print(k, '*');
            Console.WriteLine();
        }

- PP May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void PrintStarPattern(int N)
        {
            PrintPatternInternal(1, N, 2);
            PrintPatternInternal(N-2, N, -2);
        }

        private static void PrintPatternInternal(int k, int N, int delta)
        {
            if (k < 1 || k > N)
            {
                return;
            }

            PrintLine(k, N);
            PrintPatternInternal(k + delta, N, delta);
        }

        private static void Print(int n, char c)
        {
            if (n < 0)
            {
                throw new ArgumentException();
            }

            for (int i = 1; i <= n; i++)
            {
                Console.Write(c);
            }
        }

        private static void PrintLine(int k, int N)
        {
            Print((N - k)/2, ' ');
            Print(k, '*');
            Console.WriteLine();
        }

- PP May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiamondPatternOfStars
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter numer:");
            int num = Convert.ToInt32(Console.ReadLine());

            if(num % 2 == 0)
            {
                Console.WriteLine("Enter ODD number.");
                return;
            }

            PrintStar(num / 2, 1, num-1, num);
        }

        static void PrintStar(int Pos, int TotalStars, int  LeftRows, int TotalRows)
        {
            if (LeftRows < 0)
            {
                return;
            }

            for (int i = 0; i < TotalStars + Pos; i++)
            {
                if(i < Pos)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.Write("*");
                }
            }

            Console.WriteLine();

            if (LeftRows > TotalRows / 2)
            {
                PrintStar(Pos - 1, TotalStars + 2, LeftRows - 1, TotalRows);
            }
            else
            {
                PrintStar(Pos + 1, TotalStars - 2, LeftRows - 1, TotalRows);
            }
        }
    }
}

- Sairam May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiamondPatternOfStars
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter numer:");
            int num = Convert.ToInt32(Console.ReadLine());

            if(num % 2 == 0)
            {
                Console.WriteLine("Enter ODD number.");
                return;
            }

            PrintStar(num / 2, 1, num, num);
        }

        static void PrintStar(int Pos, int TotalStars, int  LeftRows, int TotalRows)
        {
            if (LeftRows < -1)
            {
                return;
            }

            for (int i = 0; i < TotalStars + Pos; i++)
            {
                if(i < Pos)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.Write("*");
                }
            }

            Console.WriteLine();

            if (LeftRows > TotalRows / 2)
            {
                PrintStar(Pos - 1, TotalStars + 2, LeftRows - 1, TotalRows);
            }
            else
            {
                PrintStar(Pos + 1, TotalStars - 2, LeftRows - 1, TotalRows);
            }
        }
    }

}

- Sairam May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiamondPatternOfStars
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter numer:");
            int num = Convert.ToInt32(Console.ReadLine());

            if(num % 2 == 0)
            {
                Console.WriteLine("Enter ODD number.");
                return;
            }

            PrintStar(num / 2, 1, num, num);
        }

        static void PrintStar(int Pos, int TotalStars, int  LeftRows, int TotalRows)
        {
            if (LeftRows < -1)
            {
                return;
            }

            for (int i = 0; i < TotalStars + Pos; i++)
            {
                if(i < Pos)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.Write("*");
                }
            }

            Console.WriteLine();

            if (LeftRows > TotalRows / 2)
            {
                PrintStar(Pos - 1, TotalStars + 2, LeftRows - 1, TotalRows);
            }
            else
            {
                PrintStar(Pos + 1, TotalStars - 2, LeftRows - 1, TotalRows);
            }
        }
    }
}

- Sairam May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiamondPatternOfStars
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter numer:");
            int num = Convert.ToInt32(Console.ReadLine());

            if(num % 2 == 0)
            {
                Console.WriteLine("Enter ODD number.");
                return;
            }

            PrintStar(num / 2, 1, num, num);
        }

        static void PrintStar(int Pos, int TotalStars, int  LeftRows, int TotalRows)
        {
            if (LeftRows < -1)
            {
                return;
            }

            for (int i = 0; i < TotalStars + Pos; i++)
            {
                if(i < Pos)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.Write("*");
                }
            }

            Console.WriteLine();

            if (LeftRows > TotalRows / 2)
            {
                PrintStar(Pos - 1, TotalStars + 2, LeftRows - 1, TotalRows);
            }
            else
            {
                PrintStar(Pos + 1, TotalStars - 2, LeftRows - 1, TotalRows);
            }
        }
    }
}

- Sairam May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void print(int n) {
	using std::cout;
	// cout << "n = " << n << " Diamond shape is as follows:" << endl;
	int width = 0; // distance from center, also we always print middle *
	int half = n/2; // floored
	for(int i = 0; i < n; ++i){
		if(i <= half) {
			width++;
		} else {
			width--;
		}
		for(int j = 0; j < n; ++j){
			if(j > half - width && j < half + width){
				cout << '*';
			} else {
				cout << ' ';
			}
		}
		cout << endl;
	}
}

- david.sicong.zhou May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Javascript:

function printRow(n, dir){
	n = dir? n += 2 : n -= 2;
	dir = n == total? dir = false : dir;
	var rowS = "";
	var spacesN = (total - n) / 2;

for(var i = 1; i <= total; i++){
	var c = i <= spacesN || i > spacesN + n ? " ": "*";
	rowS += c;
}
	console.log(rowS);
if(n > 0){
	printRow(n, dir);
}
	
}
var dir = true;
var total = 151;
var x = -1;
printRow(x, dir);

- Naty May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function printRow(n, dir){
	n = dir? n += 2 : n -= 2;
	dir = n == total? dir = false : dir;
	var rowS = "";
	var spacesN = (total - n) / 2;

for(var i = 1; i <= total; i++){
	var c = i <= spacesN || i > spacesN + n ? " ": "*";
	rowS += c;
}
	console.log(rowS);
if(n > 0){
	printRow(n, dir);
}
	
}
var dir = true;
var total = 151;
var x = -1;
printRow(x, dir);

- Anonymous May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def diamond(odd, r, flag):

if (r == odd):
flag = 0


if r < 0:
return

a = ''
for i in range(r):
a+= '*'

print a
if flag:
diamond(odd, r+2, flag)

else:
diamond(odd, r-2, flag)

- code May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def diamond(odd, r, flag):

    if (r ==  odd):
        flag = 0


    if r < 0:
        return

    a = ''
    for i in range(r):
        a+= '*'

    print a
    if flag:
        diamond(odd, r+2, flag)

    else:
        diamond(odd, r-2, flag)

- code May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple recursive solution using Python.
Idea: Keep incrementing the number of stars in delta of '2' until it reaches the value of 'n' after which start decrementing the delta

def star_print(n, k=1, delta=2):
    if k < 0:
        return
    t = (n - k) / 2
    print ' ' * t + '*' * k + ' ' * t
    if k == n: 
        delta = -delta
    star_print(n, (k+delta), delta)

- Galileo May 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#given odd number. Print diamond
#o(n)  iterative

def printDiamond(n):
    spc=int(n-1)/2
    for i in range(1,n+2,2):
        print spc*' '+ i*'*'
        spc=spc-1
    spc=1
    for j in range(n,0,-2):
        if j==n:
            continue
        print spc*' '+ j*'*'
        spc=spc+1

printDiamond(5)
print "Next item"
#printDiamond(7)


def printDiagmonRecUtil(n,spc,spcPrev,i):
    #if n == 5:

    #  printDiagmonRecUtil(n,1)
    if i!=n and spcPrev>=spc:
        print spc * ' ' + i * '*'
        printDiagmonRecUtil(n, spc - 1, spcPrev, i + 2)
    elif i<0 or n<0:
        return
    elif i==n:
        print spc*' '+  n*'*'
        spcPrev=spc
        spc=spc+1
        n=n-2
        i=n
        printDiagmonRecUtil(n, spc, spcPrev, i)



def printDiamondRec(n):
    spc=int(n-1)/2
    printDiagmonRecUtil(n,spc,spc,1)

printDiamondRec(5)
printDiamondRec(7)

- @TryHard May 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yet another solution in python!!

from itertools import chain
from __future__ import print_function

def print_diamond(n):
    number_of_stars = list(chain(xrange(1, n+1, 2), 
                                 xrange(n-2, 0, -2)))
    stars = []
    for e in number_of_stars:
        half = (n - e) / 2
        stars.append(' ' * half + '*' * e + ' ' * half)
    map(lambda x: print(x), stars)

- Fernando May 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Scala

object DiamondOddNumber {
 
  def main(args: Array[String]): Unit = {           
    printDiamondPattern(7)
  }
  
  def printDiamondPattern(num:Int) = {
    def f(n:Int) = {
        val whitespace = (num - n) / 2        
        1.to(whitespace).foreach(elem => print(' '))        
        
        1.to(n).foreach(elem =>  
          if (elem % 2 == 1) print('o')
          else print('*')
        )
        
        1.to(whitespace).foreach(elem => print(' '))        
        println
    }
    
    1.to(num).filter(n => n % 2 != 0).foreach(n => f(n))
    (num-1).to(1).by(-1).filter(n => n % 2 != 0).foreach(n => f(n))    
  }
  
}

- guilhebl May 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void drawing(int n) {
		int middler = n/2;
		for (int i=0 ; i < n ; i++) {
			int nonPaint = Math.abs(middler-i);
			for (int j=0; j < n ; j++) {
				if (j < nonPaint || n-j-1 < nonPaint) {
					System.out.print(" ");
				} else {
					System.out.print("*");
				}
			}
			
			System.out.println("");
		}
	}

- ethioer May 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printDiamond(int num)
	{
		for(int i=0;i<num;i++)
		{
			for(int j=0;j<num;j++)
			{
				if(j<Math.abs((num/2)-i) || j>=num-(Math.abs((num/2)-i)))
				System.out.print(" ");
				else
					System.out.print("*");	
			}
			System.out.print("\n");
		}

}

- unnikrishnankavungalanat May 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c# implementation using recursion

public static void PrintDiamond(int n)
{
	if (n <= 0 || n % 2 == 0)
		return;
	
	PrintDiamond(5, 1);
}

private static void PrintDiamond(int n, int curr)
{
	
	Console.WriteLine(s);
	
	if (curr == n)
		return;
		
	PrintDiamond(n, curr + 2);
	Console.WriteLine(s);
}

private static string GeneratePattern(int n, int curr)
{
	var sb = new StringBuilder();
	int diff = (n - curr) / 2;
	for (int i=0; i < diff; i++)
		sb.Append(' ');
	for (int i=0; i < curr; i++)
		sb.Append('*');
	for (int i=0; i < diff; i++)
		sb.Append(' ');

	return sb.ToString();
}

- hnatsu May 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintDiamondShape {

public static void main(String[] args) {
// TODO Auto-generated method stub

int num =9;
int l=1;
for (int i = 0; i <=num/2; i++) {
for (int j = i; j < num/2; j++) {
System.out.print(" ");
}
for (int k = 0; k <l; k++) {
System.out.print("*");
}
for (int j = i; j < num/2; j++) {
System.out.print(" ");
}
System.out.println();
l= l+2;
}
int x=0;
for (int i = 0; i <num/2; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
for (int k = 0; k <num-2-x; k++) {
System.out.print("*");
}
x=x+2;
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.println();

}

}

}

- undefined May 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My recursive solution :

def line(n,l):
	arr = [" "] * l
	tmp = "".join(arr)
	for i in xrange(n):
		tmp = tmp + "*"
	return tmp

def recprint(n,m,b,l=0):
	if n > 0:
		recprint(n-2,0,b,l+1)
		print line(n,l)
		if m == 0: return
	if m != 0:
		if n==b: l = (n-1)/2
		if m >= b: return
		recprint(0,m+2,b,l-1)
		print line(m,l)


recprint(11,1,11)

- nunziomeli5 May 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void print(int num) {
int upCounter = 1;
int downCounter = num;
print(num, upCounter, downCounter);

}

private static void print(int num, int upCounter, int downCounter) {

if(upCounter > num)
return;

print(num, upCounter+1, downCounter-1);

int spaces = Math.abs(upCounter - downCounter);
for(int i = 0; i < spaces/2; i++)
System.out.print(" ");

int stars = num - spaces;
for(int i = 0; i < stars; i ++)
System.out.print("*");

System.out.println();
}

- Anonymous May 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void print(int num) {
        int upCounter = 1;
        int downCounter = num;
        print(num, upCounter, downCounter);

    }

    private static void print(int num, int upCounter, int downCounter) {

        if(upCounter > num)
            return;

        print(num, upCounter+1, downCounter-1);

        int spaces = Math.abs(upCounter - downCounter);
        for(int i = 0; i < spaces/2; i++)
            System.out.print(" ");

        int stars = num - spaces;
        for(int i = 0; i < stars; i ++)
            System.out.print("*");

        System.out.println();
    }

- Anonymous May 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void print(int num) {
        int upCounter = 1;
        int downCounter = num;
        print(num, upCounter, downCounter);

    }

    private static void print(int num, int upCounter, int downCounter) {

        if(upCounter > num)
            return;

        print(num, upCounter+1, downCounter-1);

        int spaces = Math.abs(upCounter - downCounter);
        for(int i = 0; i < spaces/2; i++)
            System.out.print(" ");

        int stars = num - spaces;
        for(int i = 0; i < stars; i ++)
            System.out.print("*");

        System.out.println();

}

- Emma May 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class diamondShapeStars {

    public static void printStar(int n, int diff, int i){
        if(i == n) diff = -diff;
        if(i<1) return;

        int space = (n-i)/2;
        for(int x = 0 ; x < space; x++) System.out.print(" ");
        for(int x = 0 ; x < i; x++) System.out.print("*");
        for(int x = 0 ; x < space; x++) System.out.print(" ");

        System.out.println("");
        printStar(n, diff, i+diff);
    }

    public static void main(String args[]){
        printStar(5, 2, 1);
    }
}

- Aravind May 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class diamondShapeStars {

    public static void printStar(int n, int diff, int i){
        if(i == n) diff = -diff;
        if(i<1) return;

        int space = (n-i)/2;
        for(int x = 0 ; x < space; x++) System.out.print(" ");
        for(int x = 0 ; x < i; x++) System.out.print("*");
        for(int x = 0 ; x < space; x++) System.out.print(" ");

        System.out.println("");
        printStar(n, diff, i+diff);
    }

    public static void main(String args[]){
        printStar(5, 2, 1);
    }
}

- aravindbros May 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int main()
{
    int size=8;
    int i=0;

    for (i=1;i<size;i++) {
        for (int j=size;j>i;j--) {
            cout<<" ";
        }   

        for (int k=0;k<i;k++) {
            cout<<" *";
        }   
        cout<<endl;
    }   

    for (i=size;i>0;i--) {
        for (int j=size;j>i;j--) {
            cout<<" ";
        }   

        for (int k=0;k<i;k++) {
            cout<<" *";
        }   

        cout<<endl;
    }   
    return 0;
}

- Maddy September 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

## Python
n = int(input())




for i in range(1,n+1,2):
print(" "*(n-i+1),"*" *(i))
for i in range(n-2, -1, -2):
print(" "*(n-i+1), "*"*(i))

- Arvind June 01, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
int main()
{
int n, c, k, space = 1;
printf("Enter number of rows\n");
scanf("%d", &n);
int s = n/2;
n = n-s;

space = n - 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space--;
for (c = 1; c <= 2*k-1; c++)
printf("*");
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*");
printf("\n");
}
return 0;
}

- girlwhocodeit September 06, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
int main()
{
int n, c, k, space = 1;
printf("Enter number of rows\n");
scanf("%d", &n);
int s = n/2;
n = n-s;

space = n - 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space--;
for (c = 1; c <= 2*k-1; c++)
printf("*");
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*");
printf("\n");
}
return 0;
}

- girlwhocodeit September 06, 2022 | 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