Pattern-based coding questions are a common way to assess a candidate’s logical thinking, loop understanding, and ability to derive mathematical relationships from a problem statement.
One such question is the Reverse Pyramid Pattern.
Problem Statement
Write a Java program that takes an integer N as input and prints a reverse pyramid.
Example Input
5
Expected Output
* * * * *
* * * *
* * *
* *
*
Requirements
- The first row should contain
Nstars separated by spaces. - Each subsequent row should:
- Have one additional leading space.
- Have one less star than the previous row.
- The output should visually resemble a reverse pyramid.
Understanding the Pattern
Let’s analyze the output for N = 5.
| Row | Leading Spaces | Number of Stars |
|---|---|---|
| 1 | 0 | 5 |
| 2 | 1 | 4 |
| 3 | 2 | 3 |
| 4 | 3 | 2 |
| 5 | 4 | 1 |
From this table we can derive:
Leading Spaces
spaces = rowNumber
Number of Stars
stars = N - rowNumber
where row numbering starts from 0.
Java Solution
public class ReversePyramid {
public static void main(String[] args) {
int n = 5;
for (int row = 0; row < n; row++) {
// Print leading spaces
for (int space = 0; space < row; space++) {
System.out.print(" ");
}
// Print stars separated by spaces
for (int star = 0; star < n - row; star++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Dry Run
Row 0
spaces = 0
stars = 5
Output:
* * * * *
Row 1
spaces = 1
stars = 4
Output:
* * * *
Row 2
spaces = 2
stars = 3
Output:
* * *
And so on until the last row.
Algorithm
- Read input integer
N. - Iterate from row
0toN-1. - Print
rownumber of leading spaces. - Print
(N-row)stars separated by spaces. - Move to the next line.
- Repeat until all rows are printed.
Time Complexity Analysis
Outer Loop
The outer loop executes:
N times
Inner Loops
For every row:
- Space loop executes
rowtimes. - Star loop executes
(N-row)times.
Total work per row:
row + (N-row) = N
Since each row performs approximately N operations and there are N rows:
Total Operations = N × N
Time Complexity
O(N²)
Space Complexity
O(1)
No additional data structures are used.
Interview Discussion Points
After the candidate solves the problem, consider asking the following questions.
Question 1
Why is the time complexity O(N²)?
Expected Answer
Because for every row we perform approximately N operations and there are N rows.
Question 2
Can you print the normal pyramid instead?
Example:
*
* *
* * *
* * * *
* * * * *
Expected Answer
Candidate should modify both the space and star calculations.
Question 3
Can you print a diamond pattern?
Example:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Expected Answer
Combine a normal pyramid and a reverse pyramid.
Question 4
Can you solve this using StringBuilder?
Expected Answer
Build each row in memory before printing to reduce repeated console writes.
Question 5
What happens if N is negative or zero?
Expected Answer
Validate input before processing and handle invalid values appropriately.
What This Question Evaluates
This simple-looking problem helps interviewers evaluate multiple skills:
- Nested loop understanding
- Pattern recognition
- Logical reasoning
- Dry-run capability
- Mathematical thinking
- Time complexity analysis
- Code readability