A hollow diamond inside a square frames a hollow diamond with solid top and bottom bars: every line is 2 * rows characters wide, and height is 2 * rows - 1.
Remember
Rule: solid bars on ends; elsewhere left * + gap + right *
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
********** ← rows = 5 (width 10, height 9)
Unlike Program 9 (hollow diamond alone) or Program 10 (filled diamond), middle rows here are always left stars, gap spaces, then left stars again — with a mirrored i so the hollow opens to the waist and closes again.
Approach
How to Solve It
Two ways to emit the same shape — start with segment loops, then optionally shorten with String.repeat (Java 11+).
Method
Idea
Best for
Three-segment loops
Solid bars on ends; left / gap / right inside
Learning, interviews, exams
String.repeat segments
Build each bar or segment in one call
Shorter demos once formulas click
Pseudocode
Pseudocode
height = 2 * rows - 1
width = 2 * rows
for line from 1 to height:
if line is first or last:
print width stars
else:
i = line if line <= rows else (2 * rows - line)
left = rows - i + 1
gap = 2 * (i - 1)
print left stars, gap spaces, left stars
print newline
Cheat sheet
Goal
Pattern
Dimensions
height = 2 * rows - 1, width = 2 * rows
Solid bar
if (line == 1 || line == height) print width stars
Map line → i
i = (line <= rows) ? line : (2 * rows - line)
Left / right stars
left = rows - i + 1
Hollow gap
gap = 2 * (i - 1)
Width check
2 * left + gap == width
One-line bar shortcut
System.out.println("*".repeat(width));
Printing Stars vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each * and each space
System.out.println
Ends the current line
After the bar or the three segments
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the size and the framed hollow diamond updates instantly — including width and height.
Whole numbers from 1 to 10. Width is 2 * rows; height is 2 * rows - 1.
1. Set the grid.height = 9 and width = 10 for rows = 5.
2. Solid bars. When line is 1 or 9, print ten stars with System.out.print.
3. Map the inner index. For other lines, i = line on the way down, or 2 * rows - line on the way up.
4. Print three segments.left stars, gap spaces, left stars — then println.
On the waist (line = 5), i = 5, so left = 1 and gap = 8: one star on each side with a wide hollow center.
Example 2 — User Input Version
Read the size at runtime with Scanner. Prefer hasNextInt in real apps (shown in the tip below).
Java
import java.util.Scanner;
class DiamondInSquareInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
int height = 2 * rows - 1;
int width = 2 * rows;
for (int line = 1; line <= height; line++) {
if (line == 1 || line == height) {
for (int j = 1; j <= width; j++) {
System.out.print("*");
}
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (int j = 1; j <= left; j++) {
System.out.print("*");
}
for (int j = 1; j <= gap; j++) {
System.out.print(" ");
}
for (int j = 1; j <= left; j++) {
System.out.print("*");
}
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
********
*** ***
** **
* *
** **
*** ***
********
How It Works
1. Prompt and read. Ask for a size, then sc.nextInt() stores the integer.
2. Same grid core. Only the source of rows changes — the bar and left/gap/right logic match Example 1.
3. Safer input tip.nextInt() throws on letters. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive whole number.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive whole number.");
return;
}
Example 3 — String.repeat Segments (Java 11+)
Build the solid bar and each left / gap / right piece as strings — same shape, fewer inner loops.
Java
class DiamondInSquareRepeat {
public static void main(String[] args) {
int rows = 5;
int height = 2 * rows - 1;
int width = 2 * rows;
for (int line = 1; line <= height; line++) {
if (line == 1 || line == height) {
System.out.println("*".repeat(width));
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
System.out.print("*".repeat(left));
System.out.print(" ".repeat(gap));
System.out.println("*".repeat(left));
}
}
}
}
1. Same outer loop. Still walk line from 1 to height with the same bar vs inner branch.
2. Build each segment."*".repeat(left) and " ".repeat(gap) replace the character loops.
3. Print and advance. Use print for the first two segments and println for the last so the row ends correctly.
Learn the loop version first (Examples 1–2) so you can explain every bound in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
Width vs height
Do not swap formulas
Width is 2 * rows; height is 2 * rows - 1. Mixing them skews the whole frame.
Wrong mirror
Broken lower half
Use i = (line <= rows) ? line : (2 * rows - line). Forgetting the mirror breaks symmetry.
Program 9 logic
Different layout
Diagonal i == j tests from Program 9 do not draw this framed figure — use left / gap / right.
rows = 1
Single bar
Height = 1, width = 2 — output is just ** (first line is also the last).
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
Check hasNextInt
nextInt() throws on letters — prefer sc.hasNextInt() first.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
String.repeat segments (Example 3)
O(rows²)
O(rows) temporary per segment
About 2n - 1 lines × 2n characters per line for n = rows — still quadratic in n.
Remember
Key Takeaways
Grid: width 2n, height 2n - 1.
Bars: solid top and bottom; elsewhere left / gap / right.
Mirror: map line → i, then left = rows - i + 1 and gap = 2 * (i - 1).
Complexity:O(n²) time; O(1) extra space for nested loops.
One line: solid bars on the ends; elsewhere print left stars, gap spaces, left stars — keep 2 * left + gap == width.
Frequently Asked Questions
Use height 2*rows-1 and width 2*rows. Print a full row of stars on the first and last lines. For every other line, map line to i with symmetry, then print (rows-i+1) stars, a gap of 2*(i-1) spaces, and the same number of stars again.
Width is 2*rows and height is 2*rows-1 so the top and bottom are full horizontal bars while the sides close on the leftmost and rightmost columns of the inner rows.
Program 9 prints a hollow diamond alone with constant width 2*rows-1. Program 11 adds solid top and bottom bars of length 2*rows and builds each inner line from left stars, a gap, and right stars.
left = rows - i + 1 is how many stars sit on each side. gap = 2 * (i - 1) is the hollow space between them. Together they always sum to width.
If line <= rows, i = line. Otherwise i = 2 * rows - line. That mirrors the distance from the nearest end so the hollow waist is widest in the middle.
System.out.print stays on the same line. System.out.println ends the current line. Stars and spaces use print; the row break uses println after the bar or the three segments.
O(n²) where n is rows. There are 2n-1 lines and each prints 2n characters.
Check sc.hasNextInt() before sc.nextInt() and require rows >= 1 so bad input does not throw InputMismatchException.
🤔
Did you know?
Every line is exactly 2 * rows characters wide. Inner rows always satisfy 2 * left + gap == 2 * rows — so the frame closes cleanly on both sides.