Inverted Pyramid
Java coding interview problem for Pattern Printing: Inverted Pyramid.
Pattern programming is one of the most frequently asked topics in Java interviews, especially for beginners and fresh graduates.
The Inverted Pyramid Pattern is a variation of the Full Pyramid Pattern where the width decreases in every row.
This problem helps interviewers evaluate your understanding of:
- Nested loops
- Loop execution flow
- Row and column concepts
- Space management
- Logical thinking
- Pattern recognition
Mastering the Inverted Pyramid also helps in solving more advanced pattern problems such as:
- Hollow Inverted Pyramid
- Diamond Pattern
- Hourglass Pattern
- Butterfly Pattern
- Sandglass Pattern
What is an Inverted Pyramid Pattern?
An Inverted Pyramid Pattern is a triangular pattern where the number of stars decreases in each row while the leading spaces increase.
For example,
*********
*******
*****
***
*
This is called a Full Inverted Pyramid Pattern.
Why Are Pattern Programs Asked in Interviews?
Interviewers use pattern programs to evaluate your ability to:
- Analyze problems
- Work with nested loops
- Understand row-column relationships
- Calculate spaces and symbols
- Build logical solutions
Pattern problems improve your ability to solve matrix and grid-based programming questions.
Understanding Rows and Columns
Suppose
Rows = 5
Output
*********
*******
*****
***
*
Observe the number of stars.
| Row | Stars |
|---|---|
| 1 | 9 |
| 2 | 7 |
| 3 | 5 |
| 4 | 3 |
| 5 | 1 |
The stars follow
2 × (Rows − CurrentRow) + 1
or
2 × i − 1
when iterating from
Rows
↓
1
Now observe the spaces.
| Row | Leading Spaces |
|---|---|
| 1 | 0 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
The spaces follow
CurrentRow − 1
Mathematical Formula
For every row
Leading Spaces
Current Row − 1
Stars
2 × (Rows − Current Row) + 1
These formulas help generate the complete inverted pyramid.
Visual Representation
For
Rows = 5
Row 1
Spaces = 0
Stars = 9
*********
-------------------
Row 2
Spaces = 1
Stars = 7
*******
-------------------
Row 3
Spaces = 2
Stars = 5
*****
-------------------
Row 4
Spaces = 3
Stars = 3
***
-------------------
Row 5
Spaces = 4
Stars = 1
*
Pattern Output
Input
Rows = 5
Output
*********
*******
*****
***
*
Understanding the Logic
Each row contains two parts.
First,
print spaces.
Then,
print stars.
Example
Row 3
Spaces
↓
2
Stars
5
Output
*****
The same process repeats until only one star remains.
Brute Force Approach
The easiest solution uses three loops.
Loop 1
Print every row.
Loop 2
Print leading spaces.
Loop 3
Print stars.
Algorithm
Step 1
Read the number of rows.
rows = 5;
Step 2
Start the outer loop.
Rows
↓
1
Step 3
Print spaces.
rows - currentRow
or
rows - i
times.
Step 4
Print stars.
2 * currentRow - 1
times.
Step 5
Move to the next line.
Repeat until all rows are printed.
Dry Run
Input
Rows = 4
Iteration 1
Spaces = 0
Stars = 7
*******
Iteration 2
Spaces = 1
Stars = 5
*****
Iteration 3
Spaces = 2
Stars = 3
***
Iteration 4
Spaces = 3
Stars = 1
*
Approach 1 — Using Nested Loops
This is the standard interview solution.
Complete Java Program
public class InvertedPyramidPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
*********
*******
*****
***
*
Step-by-Step Code Explanation
Step 1
Declare the number of rows.
int rows = 5;
Current value
5
Step 2
Create the outer loop.
for (int i = rows; i >= 1; i--)
This loop controls the rows in reverse order.
Iterations
5
4
3
2
1
Step 3
Print leading spaces.
for (int j = rows; j > i; j--)
Spaces printed
| Row | Spaces |
|---|---|
| 1 | 0 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
Step 4
Print stars.
for (int k = 1; k <= (2 * i - 1); k++)
Stars printed
| Iteration | Stars |
|---|---|
| 5 | 9 |
| 4 | 7 |
| 3 | 5 |
| 2 | 3 |
| 1 | 1 |
Step 5
Move to the next line.
System.out.println();
Without this statement, every row would appear on the same line.
Example Execution
Input
Rows = 3
Output
*****
***
*
Input
Rows = 6
Output
***********
*********
*******
*****
***
*
Why Does This Work?
The algorithm divides each row into two parts:
- Leading Spaces increase by one in every row.
- Stars decrease by two in every row.
The outer loop controls the rows, while the two inner loops independently print spaces and stars. This combination gradually shrinks the pyramid from top to bottom, producing the inverted shape.
Advantages of This Approach
- Easy to understand.
- Uses simple nested loops.
- Frequently asked in coding interviews.
- Easy to extend into Hollow and Diamond patterns.
- Uses only constant extra memory.
Drawbacks
Although this is the standard interview solution, interviewers often ask follow-up questions such as:
- Can you create a reusable
printInvertedPyramid()method? - Can you print a Hollow Inverted Pyramid?
- Can you print an Inverted Number Pyramid?
- Can you print an Inverted Alphabet Pyramid?
- What is the time complexity?
- Can you generate the pattern using recursion?
In Part 2, we'll cover:
- Reusable
printInvertedPyramid()Method - Hollow Inverted Pyramid
- Inverted Number Pyramid
- Inverted Alphabet Pyramid
- Time and Space Complexity
- Comparison of Approaches
- Common Interview Mistakes
- Interview Follow-up Questions
- Related Pattern Problems
- Key Takeaways
- Interview Tips
Approach 2 — Using a Reusable Method
Instead of writing the logic directly inside the main() method, we can create a reusable method.
This approach improves:
- Code readability
- Reusability
- Maintainability
- Unit testing
Java Program
public class InvertedPyramidMethod {
static void printInvertedPyramid(int rows) {
for (int i = rows; i >= 1; i--) {
// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
printInvertedPyramid(5);
}
}
Output
*********
*******
*****
***
*
Pattern Variation 1 — Hollow Inverted Pyramid
Instead of printing stars at every position, print only the border.
Output
*********
* *
* *
* *
*
Java Program
public class HollowInvertedPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
if (i == rows || i == 1 || k == 1 || k == (2 * i - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Pattern Variation 2 — Inverted Number Pyramid
Replace stars with numbers.
Output
123454321
1234321
12321
121
1
Java Program
public class InvertedNumberPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Ascending numbers
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
// Descending numbers
for (int j = i - 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
Pattern Variation 3 — Inverted Alphabet Pyramid
Replace stars with alphabets.
Output
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
Java Program
public class InvertedAlphabetPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
// Print leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Ascending alphabets
for (char ch = 'A'; ch < 'A' + i; ch++) {
System.out.print(ch);
}
// Descending alphabets
for (char ch = (char) ('A' + i - 2); ch >= 'A'; ch--) {
System.out.print(ch);
}
System.out.println();
}
}
}
Dry Run
Input
Rows = 3
Processing
Row 1
Spaces = 0
Stars = 5
*****
---------------
Row 2
Spaces = 1
Stars = 3
***
---------------
Row 3
Spaces = 2
Stars = 1
*
Time Complexity
Suppose
n
is the number of rows.
Standard Inverted Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Hollow Inverted Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Number Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Alphabet Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Comparison of Approaches
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Basic Nested Loops | O(n²) | O(1) | ✅ Best for Beginners |
| Reusable Method | O(n²) | O(1) | Production Ready |
| Hollow Inverted Pyramid | O(n²) | O(1) | Intermediate |
| Number Pyramid | O(n²) | O(1) | Interview Favorite |
| Alphabet Pyramid | O(n²) | O(1) | Interview Favorite |
Common Mistakes
Mistake 1
Printing the wrong number of spaces.
Wrong
for (int j = 1; j <= rows; j++)
Correct
for (int j = rows; j > i; j--)
Mistake 2
Printing the wrong number of stars.
Wrong
k <= rows
Correct
k <= (2 * i - 1)
Mistake 3
Forgetting to print a new line.
Wrong
System.out.print();
Correct
System.out.println();
Without this statement, the entire pattern appears on one line.
Mistake 4
Incorrect outer loop direction.
Wrong
for (int i = 1; i <= rows; i++)
Correct
for (int i = rows; i >= 1; i--)
The outer loop must move from the largest row to the smallest.
Mistake 5
Confusing row count with star count.
Remember:
Rows
↓
Controls height
Stars
↓
2 × Row − 1
Interview Follow-up Questions
Q1. Why do we print spaces before stars?
Q2. Why does the number of stars decrease by two?
Q3. Can you print a Hollow Inverted Pyramid?
Q4. Can you print an Inverted Number Pyramid?
Q5. Can you print an Inverted Alphabet Pyramid?
Q6. What is the time complexity?
Q7. Can you solve this using recursion?
Q8. How is an Inverted Pyramid different from a Full Pyramid?
Q9. Can you combine both to print a Diamond Pattern?
Q10. Can you write a reusable printInvertedPyramid() method?
Related Pattern Problems
- Full Pyramid Pattern
- Hollow Pyramid
- Right Triangle Pattern
- Left Triangle Pattern
- Diamond Pattern
- Butterfly Pattern
- Number Pyramid
- Alphabet Pyramid
- Pascal's Triangle
- Floyd's Triangle
Key Takeaways
- An Inverted Pyramid Pattern decreases the number of stars in every row.
- Every row consists of:
- Leading spaces
- Stars (or numbers/alphabets)
- Leading spaces follow:
Current Row − 1
- Stars follow:
2 × (Rows − Current Row) + 1
or equivalently,
2 × i − 1
when iterating from rows down to 1.
- Always print spaces before stars.
- The standard solution runs in O(n²) time and uses O(1) extra space.
Frequently Asked Interview Questions
Q1. Why are nested loops required?
The outer loop controls the rows, while the inner loops print the required number of spaces and stars for each row.
Q2. Why do the stars decrease by two in every row?
Each new row removes one star from the left side and one star from the right side.
Example
| Row | Stars |
|---|---|
| 1 | 9 |
| 2 | 7 |
| 3 | 5 |
| 4 | 3 |
| 5 | 1 |
Q3. Why do the spaces increase in every row?
Increasing the leading spaces shifts the stars to the right, creating the inverted pyramid shape.
Q4. Can we print numbers or alphabets instead of stars?
Yes.
Replace
System.out.print("*");
with the required number or alphabet printing logic while keeping the loop structure unchanged.
Q5. What is the difference between a Full Pyramid and an Inverted Pyramid?
| Full Pyramid | Inverted Pyramid |
|---|---|
| Stars increase in every row. | Stars decrease in every row. |
| Spaces decrease in every row. | Spaces increase in every row. |
| Top is narrow and bottom is wide. | Top is wide and bottom is narrow. |
Interview Tip
If an interviewer asks:
"Print an Inverted Pyramid Pattern in Java."
Start by explaining that each row has two sections:
- Print the required number of leading spaces.
- Print the required number of stars, which decreases by 2 in every row.
Use a nested-loop solution with the outer loop iterating from the last row to the first. Mention that the same logic can be extended to create Hollow Inverted Pyramids, Number Pyramids, Alphabet Pyramids, Hourglass Patterns, and Diamond Patterns by modifying only the inner-loop printing logic while keeping the overall structure the same.