Grid Size
2n × 2n−1
Width 2 * rows, height 2 * rows - 1 — a wide frame for the diamond.

This pattern frames a hollow diamond inside solid top and bottom bars: fixed width 2 * rows, height 2 * rows - 1, and mirrored middle rows built from left stars, a gap, and right stars. This tutorial covers the grid size, border vs inner logic, a live preview, algorithm steps, worked Java examples, edge cases, and complexity.
2n × 2n−1
Width 2 * rows, height 2 * rows - 1 — a wide frame for the diamond.
Top & bottom
First and last lines are full runs of * with no interior gap.
Inner rows
Equal star blocks on both sides with a hollow gap between them.
iSymmetry
Map each line to i so the gap grows to the waist, then shrinks.
1–10 rows
Pick a size and draw the framed hollow diamond in the browser.
Complexity
2n-1 lines × 2n columns — O(n²) time, O(1) extra space.
A hollow diamond inside a square is a framed console figure: solid star bars on the first and last lines, and a hollow diamond carved through the middle rows.
Unlike Program 9 (standalone hollow diamond) or Program 10 (filled diamond), every line here is exactly 2 * rows characters wide, and middle rows are built as left stars + gap + right stars.
It trains fixed-width grids, border special cases, and mirrored indices in one figure — a strong capstone after simpler triangles and diamonds.
Every line is 2 * rows characters.
Top and bottom bars close the “square” frame.
Left stars, hollow gap, right stars.
i grows then shrinks with line position.
In short: solid bars on the ends; elsewhere print left stars, gap spaces, left stars — with i mirrored so the hollow diamond opens and closes.
Given a positive integer rows, print a framed hollow diamond with width 2 * rows and height 2 * rows - 1.
// rows = 5 (conceptual shape; spaces shown as ·)
// **********
// ****··****
// ***····***
// **······**
// *········*
// **······**
// ***····***
// ****··****
// ********** | Item | Type | Description |
|---|---|---|
rows | int | Size parameter (typically ≥ 1). Controls both width and height. |
| Printed output | text | 2*rows-1 lines, each exactly 2*rows characters before the newline. |
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
newline | Approach | Idea | Best for |
|---|---|---|
| Three-segment loops | Left / gap / right on inner rows | Learning and interviews |
printChars helper | Build each segment as a string | Shorter demos after formulas click |
| 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 |
Three diamond-related patterns — different frames and fills.
framed hollowSolid bars + left/gap/right; width 2n
hollow aloneOutline only; fixed width 2n-1 per line
solid diamondFull star runs; tip rows shorter than middle
state dimensionsSay width/height first, then border vs inner cases
Reach for this figure when you need a fixed-width frame with a hollow diamond interior.
Often the last numbered exercise after triangles and diamonds.
Every line must close at the same column count.
Special-case first/last rows; formula-drive the middle.
Practice folding a line index into a mirrored i.
Terminal teaching pattern — not how you build framed widgets in apps.
Key benefit: one pattern that combines dimensions, border cases, segment math, and mirrored indices.
Choose a size between 1 and 10 and draw the framed hollow diamond in the browser.
Three complete Java programs — fixed size, console input, and a printChars helper. Click View Output to reveal sample console results.
Print the classic rows = 5 framed diamond with nested loops.
rows = 5Solid bars on the ends; left / gap / right on every other line.
public class DiamondInSquare {
public static void main(String[] args) {
int rows = 5;
int line, j;
int height = 2 * rows - 1;
int width = 2 * rows;
for (line = 1; line <= height; line++) {
if (line == 1 || line == height) {
for (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 (j = 1; j <= left; j++) {
System.out.print("*");
}
for (j = 1; j <= gap; j++) {
System.out.print(" ");
}
for (j = 1; j <= left; j++) {
System.out.print("*");
}
}
System.out.println();
}
}
} Lines 1 and 9 are solid 10-star bars. On line 5 (the waist), i = 5, so left = 1 and gap = 8 — a single star on each side with a wide hollow center. Lines above and below mirror that formula via the ternary for i.
Let the user choose the size at runtime.
Read rows with Scanner and nextInt() (check hasNextInt() in real apps).
import java.util.Scanner;
public class DiamondInSquareInput {
public static void main(String[] args) {
int rows;
int line, j;
int height, width;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
rows = sc.nextInt();
height = 2 * rows - 1;
width = 2 * rows;
for (line = 1; line <= height; line++) {
if (line == 1 || line == height) {
for (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 (j = 1; j <= left; j++) {
System.out.print("*");
}
for (j = 1; j <= gap; j++) {
System.out.print(" ");
}
for (j = 1; j <= left; j++) {
System.out.print("*");
}
}
System.out.println();
}
sc.close();
}
} Same grid logic as Example 1; only the source of rows changes. For rows = 4 you get 7 lines × 8 columns. Non-numeric input throws InputMismatchException with nextInt() — check hasNextInt() for safer labs.
Same figure without explicit character loops for each segment.
printChars HelperBuild the solid bar and each left / gap / right piece as strings.
public class DiamondInSquareHelper {
static void printChars(char ch, int n) {
System.out.print(String.valueOf(ch).repeat(n));
}
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) {
printChars('*', width);
System.out.println();
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
printChars('*', left);
printChars(' ', gap);
printChars('*', left);
System.out.println();
}
}
}
} Same formulas as Example 1; printChars replaces the three inner loops. Keep the loop version for exams that want every bound visible.
height = 2 * rows - 1 lines; width = 2 * rows characters per line. For rows = 4: 7 × 8.
When line == 1 or line == height, print a solid run of width stars — the closed horizontal edges.
Map line → i, then left = rows - i + 1 and gap = 2 * (i - 1). Print left stars, gap spaces, left stars again.
System.out.println() after the bar or the three segments. Every line is exactly width characters before the newline.
O(n²) for n = rows (2n-1 lines × up to 2n cells), O(1) extra space.
rows = 4Trace each line: whether it is a solid bar, and if not, the values of i, left, and gap.
| line | Kind | i | left | gap | Printed row |
|---|---|---|---|---|---|
1 | Bar | — | — | — | ******** |
2 | Inner | 2 | 3 | 2 | *** *** |
3 | Inner | 3 | 2 | 4 | ** ** |
4 | Inner | 4 | 1 | 6 | * * |
5 | Inner | 3 | 2 | 4 | ** ** |
6 | Inner | 2 | 3 | 2 | *** *** |
7 | Bar | — | — | — | ******** |
Check: on every inner row, 2 * left + gap = 8 = width. Lines 3 and 5 share the same i because of mirroring.
Where this framed hollow diamond (and its grid thinking) shows up beyond the homework prompt.
Combines borders, segments, and mirroring in one program.
Example: final lab after Programs 9 and 10.
Assert 2*left + gap == width while debugging.
Example: print lengths before println.
Side-by-side with Program 9 shows why structures differ.
Example: same rows, different width rules.
One loop over columns with border/left/right predicates.
Example: for (j = 1; j <= width; j++) with booleans.
Swap border vs interior characters once geometry works.
Example: # on bars, * on sides.
Fixed-size grids make O(n²) easy to count by hand.
Example: cells = (2n-1)*2n.
Pro Tip: in interviews, state “width 2n, height 2n-1, solid caps, then left/gap/right with mirrored i” before writing a single loop.
Why this framed layout is a strong teaching pattern.
2 * left + gap == width catches off-by-one bugs immediately.
Top/bottom bars are obvious; middle rows share one formula.
A single ternary for i keeps upper and lower halves in sync.
Streaming output needs only counters and a few ints.
Pro Tip: learn the three-loop segment version first; treat printChars as a polish shortcut afterward.
Small habits that keep framed-diamond code clean.
Compute width and height once — do not mix 2*rows and 2*rows-1 by accident.
Special-case top and bottom before writing the inner formula.
Mentally check 2 * left + gap == width on a middle and a near-tip row.
hasNextInt()Avoid FormatException when reading interactive sizes.
rows = 4The walkthrough table above catches mirror and gap mistakes fast.
Pro Tip: if a line is shorter or longer than the others, you almost certainly used the wrong formula for left or gap.
Mistakes that commonly break the framed hollow diamond.
Using 2*rows-1 as width (or 2*rows as height) skews the whole figure.
→ Width is 2*rows; height is 2*rows-1.
Different coordinate system — i == j style loops will not match this 10-wide picture for rows = 5.
→ Use left / gap / right for this page.
Forgetting 2 * rows - line breaks lower-half symmetry.
→ i = (line <= rows) ? line : (2 * rows - line).
Padding after the right star block makes lines longer than width.
→ Stop after the second left-star run.
Letters or empty input throw InputMismatchException.
→ Check hasNextInt() and require rows >= 1.
Check these inputs before calling the solution done.
Height = 1, width = 2 — only one solid line ** (first == last).
3 lines × 4 columns: bar, * *, bar.
Loop never runs — validate and re-prompt.
rows < 0Treat as invalid; do not print a broken grid.
Width grows as 2n — fine for labs; may wrap on tiny terminals.
nextInt() throws — check hasNextInt().
Try these variations to lock in the pattern.
rows2n vs 2n-1j = 1..widthhasNextInt() until rows >= 12n, height 2n - 1 for n = rows.2 * left + gap == width — use that as a sanity check.rows > 0 for interactive programs; rows = 1 collapses to a single two-star bar.Quick Takeaway: solid bars on the ends; elsewhere left stars, hollow gap, right stars — with mirrored i and fixed width 2 * rows.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
printChars helper (Example 3) | O(rows²) | O(rows) temporary per segment |
About 2 * rows - 1 lines; each prints 2 * rows characters — overall Θ(n²).
The hollow diamond inside a square is a fixed-width frame: solid top and bottom bars, and mirrored left / gap / right rows in between. Once the dimensions and the i mapping click, the rest is careful segment printing.
Practice the three examples above, then browse the star-pattern hub to revisit earlier triangles and diamonds.
Width 2n, height 2n-1, solid caps, then left/gap/right with mirrored i — and keep 2*left + gap == width.
i with left and gap formulas2 * left + gap == widthhasNextInt() for interactive demos2*rows and 2*rows-1rows = 1 edge casePrint the hollow diamond inside a square the beginner-friendly way.
2n wide, 2n−1 tall
SizeSolid top & bottom
BorderLeft / gap / right
InnerMap line → i
SymmetryO(n²) time
AnalysisEvery line is exactly 2 * rows characters wide. Inner rows always satisfy 2 * left + gap == 2 * rows — so the frame closes cleanly on both sides.
Review Programs 9 and 10, then explore more Java topics from the hub.
12 people found this page helpful