An inverted V-shaped hollow pattern prints only the outline of an upside-down V: a single apex on row 1, then two stars that drift farther apart on each later row.
Remember
Rule: star when i == j (left) or i == k (right); else space
*
* *
* *
* *
* * ← 5 rows (width 9)
Unlike the filled inverted pyramid in Program 6, most cells are spaces. This outline is also the upper half of the hollow diamond — flip the outer loop in Program 8 to get the matching upright V.
Approach
How to Solve It
Two ways to emit the same outline — start with if/else legs, then optionally shorten with a conditional expression.
Method
Idea
Best for
If/else legs
Left j and right k loops; star when indices match
Learning, interviews, exams
x if c else y
Same bounds; one-line star-vs-space choice
Shorter demos once conditions click
Pseudocode
Pseudocode
for i from 1 to rows:
for j from rows down to 1:
print "*" if i == j else " "
for k from 2 to rows:
print "*" if i == k else " "
print newline
Change the height and the hollow inverted V updates instantly — including width and star count.
Whole numbers from 1 to 14. Each line is 2 * rows - 1 characters wide.
Live result5 rows · 9 stars
*
* *
* *
* *
* *
Trace
Worked Walkthrough — rows = 4
Trace where each star lands for every outer-loop value of i (line width = 7).
i
Left star (j)
Right star (k)
Stars
Printed row
1
j == 1
none (k starts at 2)
1
*
2
j == 2
k == 2
2
* *
3
j == 3
k == 3
2
* *
4
j == 4
k == 4
2
* *
Row 1 is the only single-star line — that is why the right loop must not start at k = 1. Total stars: 1 + 2 + 2 + 2 = 7 = 2×4 - 1.
Code
Python Programs
Three complete programs: classic if/else, console input, and a conditional-expression shortcut. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — left loop j = rows..1, right loop k = 2..rows, star when indices match.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(rows, 0, -1):
if i == j:
print("*", end="")
else:
print(" ", end="")
for k in range(2, rows + 1):
if i == k:
print("*", end="")
else:
print(" ", end="")
print()
Output
*
* *
* *
* *
* *
How It Works
1. Set height.rows = 5 means five outline lines (width 9).
2. Outer loop picks the row.i runs from 1 (apex) to rows (widest gap).
3. Left leg.j counts from rows down to 1; print * only when i == j.
4. Right leg, then break.k runs from 2 to rows with the same match rule, then bare print().
When i = 1 only the left loop prints a star; when i = 5 stars land at both outer columns.
Example 2 — User Input Version
Read the height at runtime. Prefer try/except ValueError in real apps (shown in the tip below).
Python
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(rows, 0, -1):
if i == j:
print("*", end="")
else:
print(" ", end="")
for k in range(2, rows + 1):
if i == k:
print("*", end="")
else:
print(" ", end="")
print()
Output (when user enters 4)
Enter the number of rows: 4
*
* *
* *
* *
How It Works
1. Prompt and read. Ask for a row count, then convert the line with int(input(...)).
2. Same left/right core. Only the source of rows changes — the leg logic matches Example 1.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Enter a positive whole number.")
raise SystemExit(1)
Example 3 — Conditional Expression
Keep both loops; compress the star-vs-space choice into one expression each.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(rows, 0, -1):
print("*" if i == j else " ", end="")
for k in range(2, rows + 1):
print("*" if i == k else " ", end="")
print()
Output
*
* *
* *
* *
* *
How It Works
1. Same outer loop. Still walk i from 1 to rows.
2. Same bounds. Left j still counts down; right k still starts at 2.
3. Shorter print."*" if i == j else " " replaces the multi-line if/else — same decision, less code.
Learn the if/else version first (Examples 1–2) so you can explain the branch in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
k = 1
Duplicate apex
Starting the right loop at k = 1 prints two stars on row 1. Keep range(2, rows + 1).
j ascending
Mirrored left leg
The left loop must count j from rows down to 1. Ascending j flips the left diagonal.
print() inside
Broken outline
If bare print("*") is inside either inner loop, each cell lands on its own line. Use end="" for cells; print() only after both loops.
rows = 1
Single apex
Output is just * — right loop never runs. A good sanity check.
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
Use try/except
int(input()) raises ValueError on letters — prefer try/except and require rows >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
If/else legs (Examples 1–2)
O(rows²)
O(1)
Conditional form (Example 3)
O(rows²)
O(1)
About n rows × 2n - 1 characters printed per row — still quadratic in n. Total stars = 2n - 1 (one apex + two per later row).
Remember
Key Takeaways
Rule: print * only when i == j (left) or i == k (right).
Two legs: left j counts down; right k starts at 2.
Break the row: call bare print() only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each row i, print a star only when the left or right index matches i — start the right loop at 2.
Frequently Asked Questions
The outer loop runs i from 1 to rows. For each row, the left loop runs j from rows down to 1 and prints a star only when i equals j. The right loop runs k from 2 to rows and prints a star only when i equals k. Every other cell is a space.
Printing columns from high j to low j places the star for row i when i equals j. As i grows, that match moves leftward in the left block, forming the descending left leg.
On row 1 the left loop already prints the apex at j equals 1. Starting k at 1 would print a second star on that row. Starting at 2 avoids duplicating the tip.
print("*", end="") stays on the same line. print() ends the current line. Stars and spaces use end=""; the row break uses print() after both inner loops.
Each line has width 2 * rows - 1: left block length rows, right block length rows - 1.
Program 8 uses the same inner loops but counts the outer loop from rows down to 1, so the wide row prints first and the legs meet at a bottom vertex.
O(n²) for n rows. Each row runs Theta(n) iterations across the two inner loops.
Wrap int(input()) in try/except ValueError so bad input does not crash the script.
🤔
Did you know?
This hollow inverted V is the upper half of the hollow diamond. Starting the right loop at k = 2 is deliberate: on row 1 the left loop already prints the apex, so k = 1 would duplicate that star.