Table of contents
1.
Introduction
1.1.
Example:
2.
Algorithm
2.1.
Code
2.2.
Time and Space Complexity
3.
Frequently Asked Questions
3.1.
What is a hollow diamond star pattern?
3.2.
What is the asymptotic time complexity of printing the hollow diamond star pattern?
3.3.
How many rows are printed if the input is n in the hollow diamond star pattern?
3.4.
For n=5, what will be the number of spaces in the upper left half of the hollow diamond star pattern for every iteration?
4.
Conclusion
Last Updated: Sep 24, 2024
Medium

Hollow Diamond Star Pattern

Author ANKIT KUMAR
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

Introduction

The Hollow Diamond Pattern is similar to the pyramid pattern problem. The Hollow Diamond Star Program is divided into two parts: upper and lower. The upper part is almost similar to the pyramid pattern problem, and the lower part is an inverted pyramid. The main difference is that the first and last rows only have one star, while the other rows have two stars.

Example:

    *    
   * *   
  *   *  
 *     * 
*       *
 *     * 
  *   *  
   * *   
    *  
You can also try this code with Online Java Compiler
Run Code

Algorithm

If we clearly observe the hollow diamond pattern, we can find that the pattern can be viewed as a combination of two pyramids. The upper half is a normal star pyramid, while the lower half is an inverted pyramid.

This is a very simple pattern problem. The algorithm is as follows:

  • Take the number of rows as input from the user.
  • We replace the * with blank spaces in the pyramid structures.
  • Only the first and last row will have one star. Others will have two stars only.
  • In the upper half, the spaces are such that the first row has n (rows) spaces, the second n-1, and so on.
  • We try to print two patterns: the upper triangle and the lower triangle.


If the algorithm is not very easy to understand, you should check out the code given below for the hollow diamond star pattern. Doing a dry run of the code would be even better.

Code

import java.util.*;
public class Main {

	public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n = sc.nextInt();
        
        int i, j;

        // code for the upper half

        // outer loop runs for n times. n= number of rows
        for (i = 1; i <= n; i++) {

            // this loop prints the preceding spaces
            for (j = n; j > i; j--) {
                System.out.print(" ");
            }
            //the first star is printed after the left preceding spaces
            System.out.print("*");


            // this loop prints the spaces in between the two stars
            // for the first and last row since there is only one star
            // therefore no space is printed in between the stars
            for (j = 1; j < (i - 1) * 2; j++) {
                System.out.print(" ");
            }

            // if it is the first row change the Line 
            // else print the second star also 
            if (i == 1) {
                System.out.println();
            } else {
                System.out.println("*");
            }
        }


        // code for the lower half

        // outer loop runs for n-1 times 
        for (i = n - 1; i >= 1; i--) {
            // this loop prints the spaces
            for (j = n; j > i; j--) {
                System.out.print(" ");
            }

            // print the star after all the left spaces are done 
            System.out.print("*");

            // print the spaces between the stars 
            // for the last row there is only one star so no space is printed
            for (j = 1; j < (i - 1) * 2; j++) {
                System.out.print(" ");
            }


            // if we reached the last row change the line. We are done.
            // else print the second star also
            if (i == 1) {
                System.out.println();
            } else {
                System.out.println("*");
            }
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Input: 

n=8
You can also try this code with Online Java Compiler
Run Code

Output:

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *
*             *
 *           *
  *         *
   *       *
    *     *
     *   *
      * *
       *
You can also try this code with Online Java Compiler
Run Code

Time and Space Complexity

The Time Complexity of the above code is O(n2).

The Space Complexity of the above code is O(1) as no extra space is used.

Frequently Asked Questions

What is a hollow diamond star pattern?

The Hollow Diamond Star Program is divided into two parts: upper and lower. The upper part is almost similar to the pyramid pattern problem, and the lower part is an inverted pyramid.

What is the asymptotic time complexity of printing the hollow diamond star pattern?

The time complexity is O(n2).

How many rows are printed if the input is n in the hollow diamond star pattern?

The total number of rows that are printed is 2n-1, n number of rows in the upper half, and n-1 number of rows in the lower half.

For n=5, what will be the number of spaces in the upper left half of the hollow diamond star pattern for every iteration?

The number of spaces will be 4,3,2,1,0 for respective iterations.

Conclusion

In this article, we have extensively discussed the hollow diamond star pattern problem and its code implementation in Java programming language.

The Hollow Diamond Star Program is divided into two parts: upper and lower. The upper part is almost similar to the pyramid pattern problem, and the lower part is an inverted pyramid.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass