Number Pyramid
Java coding interview problem for Pattern Printing: Number Pyramid.
The Number Pyramid Pattern is one of the most commonly asked number pattern programs in Java coding interviews.
Unlike star patterns, Number Pyramid patterns use numbers instead of stars, making them slightly more challenging because they involve both loop control and number generation.
Interviewers frequently ask Number Pyramid problems to evaluate a candidate's understanding of:
- Nested loops
- Pattern recognition
- Number manipulation
- Symmetry
- Mathematical thinking
Learning Number Pyramid patterns also helps in solving advanced interview questions like:
- Palindrome Number Pyramid
- Hollow Number Pyramid
- Pascal's Triangle
- Floyd's Triangle
- Continuous Number Pyramid
What is a Number Pyramid?
A Number Pyramid is a triangular arrangement of numbers where each row contains numbers arranged in a specific pattern.
The simplest Number Pyramid prints the current row number repeatedly.
Example
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
Observe:
- Every row starts with its row number.
- Every row prints the same number repeatedly.
- The pyramid is centered.
- The width increases by two positions in every row.
Types of Number Pyramid Patterns
Java interviews may ask different variations of Number Pyramid.
Some common ones include:
| Pattern | Example |
|---|---|
| Repeated Number Pyramid | 1, 2 2 2, 3 3 3 3 3 |
| Increasing Number Pyramid | 1, 1 2 1, 1 2 3 2 1 |
| Continuous Number Pyramid | 1, 2 3 4, 5 6 7 8 9 |
| Palindrome Number Pyramid | 1, 212, 32123 |
| Hollow Number Pyramid | Numbers only on boundaries |
In this article, we focus on the Repeated Number Pyramid, which serves as the foundation for all other variations.
Difference Between Star Pyramid and Number Pyramid
| Star Pyramid | Number Pyramid |
|---|---|
Uses * characters. |
Uses numbers. |
| Simpler printing logic. | Requires number generation. |
| No changing values. | Numbers change based on row or position. |
| Focuses on alignment. | Focuses on both alignment and value generation. |
Star Pyramid
*
***
*****
*******
*********
Number Pyramid
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
Why is Number Pyramid Asked in Interviews?
Interviewers ask Number Pyramid because it tests multiple programming concepts together.
They evaluate whether candidates can:
- Control nested loops.
- Print centered patterns.
- Generate numbers dynamically.
- Understand row-column relationships.
- Apply mathematical formulas.
It also serves as the base for solving more complex number-based patterns.
Understanding Number Placement
Suppose
Rows = 5
Output
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
Observe the pattern.
| Row | Printed Number | Occurrences |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 5 |
| 4 | 4 | 7 |
| 5 | 5 | 9 |
Notice:
- Row number equals the printed value.
- Number of printed values follows:
2 × Row − 1
Mathematical Pattern
Suppose
Rows = 5
Pyramid Width
For every row,
Width
=
2 × Row − 1
Example
Row 4
Width
=
2 × 4 − 1
=
7
Leading Spaces
Before printing numbers,
Rows − Current Row
spaces are printed.
Example
| Row | Leading Spaces |
|---|---|
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
| 4 | 1 |
| 5 | 0 |
Printed Number
Every position prints
Current Row
Visual Representation
For
Rows = 5
Row 1
1
-------------------
Row 2
2 2 2
-------------------
Row 3
3 3 3 3 3
-------------------
Row 4
4 4 4 4 4 4 4
-------------------
Row 5
5 5 5 5 5 5 5 5 5
Observe:
- Width increases by 2.
- Leading spaces decrease by 1.
- Every row prints the same number repeatedly.
Pattern Output
Input
Rows = 5
Output
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
Understanding the Logic
There are three main steps.
Step 1
Print leading spaces.
Example
Row 3
Spaces
2
Step 2
Print numbers.
Each row prints
2 × Row − 1
numbers.
Every printed value equals
Current Row
Step 3
Move to the next line.
Repeat until all rows are completed.
Algorithm
Step 1
Read the number of rows.
rows = 5;
Step 2
Start the outer loop.
1
↓
Rows
Each iteration prints one row.
Step 3
Print leading spaces.
Rows − Current Row
Step 4
Print numbers.
Repeat
2 × Current Row − 1
times.
Step 5
Current Row
for every position.
Step 6
Move to the next line.
Repeat until the pyramid is completed.
Dry Run
Input
Rows = 4
Row 1
Spaces
3
Numbers
1
Output
1
Row 2
Spaces
2
Numbers
2 2 2
Output
2 2 2
Row 3
Spaces
1
Numbers
3 3 3 3 3
Output
3 3 3 3 3
Row 4
Numbers
4 4 4 4 4 4 4
Output
4 4 4 4 4 4 4
Final Output
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
Approach 1 — Using Nested Loops
This is the standard interview solution.
The idea is simple:
- Print leading spaces.
- Print the row number repeatedly.
- Increase the width for every new row.
Complete Java Program
public class NumberPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Print numbers
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
Output
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
Step-by-Step Code Explanation
Step 1
Declare the number of rows.
int rows = 5;
Step 2
Create the outer loop.
for (int i = 1; i <= rows; i++)
Each iteration prints one row.
Step 3
Print leading spaces.
for (int j = i; j < rows; j++)
These spaces align the pyramid at the center.
Step 4
Print numbers.
for (int j = 1; j <= (2 * i - 1); j++)
Each row contains
2 × Row − 1
numbers.
Step 5
Print the current row number.
System.out.print(i + " ");
Every value in the current row is the same.
Example Execution
Input
Rows = 3
Output
1
2 2 2
3 3 3 3 3
Why Does This Work?
The algorithm combines three nested operations for each row:
- Leading spaces center the pyramid.
- The inner loop determines the width using the formula
2 × row − 1. - The current row number (
i) is printed repeatedly for every position.
Because the number of spaces decreases while the number of printed values increases, the output forms a perfectly centered Number Pyramid.
Advantages of This Approach
- Easy to understand.
- Uses simple nested loops.
- Excellent for learning number-based patterns.
- Frequently asked in Java interviews.
- Forms the foundation for advanced number pyramid variations.
- Uses only O(1) extra space.
Drawbacks
Although this is the standard interview solution, interviewers often ask follow-up questions such as:
- Can you create a reusable
printNumberPyramid()method? - Can you print a Palindrome Number Pyramid?
- Can you print a Hollow Number Pyramid?
- Can you generate continuous numbers instead of repeated numbers?
- What is the time complexity?
- Can you replace numbers with alphabets?
In Part 2, we'll cover:
- Optimized Approach
- Reusable
printNumberPyramid()Method - Palindrome Number Pyramid
- Hollow Number Pyramid
- Continuous Number Pyramid
- Time & Space Complexity
- Comparison of Approaches
- Common Interview Mistakes
- Interview Follow-up Questions
- Related Pattern Problems
- Key Takeaways
- Interview Tips
Approach 2 — Optimized Approach
The solution shown in Part 1 is already the optimal approach.
Every number and every space in the pyramid must be printed exactly once, so the algorithm cannot be improved beyond visiting each output position.
The optimization focuses on:
- Cleaner code
- Better readability
- Reusable methods
- Easier maintenance
- Supporting multiple Number Pyramid variations
Optimized Java Program
public class NumberPyramidOptimized {
public static void main(String[] args) {
int rows = 5;
for (int row = 1; row <= rows; row++) {
// Print leading spaces
for (int space = 1; space <= rows - row; space++) {
System.out.print(" ");
}
// Print numbers
for (int col = 1; col <= (2 * row - 1); col++) {
System.out.print(row + " ");
}
System.out.println();
}
}
}
Output
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
Approach 3 — Using a Reusable Method
Instead of placing all logic inside main(), create a reusable method.
Advantages:
- Better code organization
- Reusable in multiple programs
- Easier testing
- Cleaner implementation
Java Program
public class NumberPyramidMethod {
static void printNumberPyramid(int rows) {
for (int row = 1; row <= rows; row++) {
// Leading Spaces
for (int space = 1; space <= rows - row; space++) {
System.out.print(" ");
}
// Numbers
for (int col = 1; col <= (2 * row - 1); col++) {
System.out.print(row + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
printNumberPyramid(5);
}
}
Pattern Variation 1 — Palindrome Number Pyramid
Instead of printing the same number repeatedly, print numbers that increase and then decrease.
Output
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Another commonly used interview variation is:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
These patterns test number manipulation instead of simply repeating values.
Pattern Variation 2 — Hollow Number Pyramid
Print numbers only on the boundary.
Output
1
2 2
3 3
4 4
5 5 5 5 5 5 5 5 5
Rules:
- Print numbers on the left boundary.
- Print numbers on the right boundary.
- Print the last row completely.
- Print spaces inside.
Pattern Variation 3 — Continuous Number Pyramid
Instead of repeating row numbers, continue counting.
Output
1
2 3 4
5 6 7 8 9
10 11 12 13 14 15 16
This variation is frequently asked after the basic Number Pyramid.
Pattern Variation 4 — Floyd's Style Pyramid
Print consecutive numbers row by row.
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Although different from a centered pyramid, interviewers often compare it with Number Pyramid problems.
Pattern Variation 5 — User Input
Allow users to specify the pyramid height.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = scanner.nextInt();
printNumberPyramid(rows);
Dry Run
Input
Rows = 3
Row 1
Spaces
2
Numbers
1
Output
1
Row 2
Spaces
1
Numbers
2 2 2
Output
2 2 2
Row 3
Spaces
0
Numbers
3 3 3 3 3
Output
3 3 3 3 3
Final Output
1
2 2 2
3 3 3 3 3
Time Complexity
Suppose
n
is the number of rows.
Number Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Palindrome Number Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Hollow Number Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Continuous Number Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Comparison of Approaches
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Basic Number Pyramid | O(n²) | O(1) | Best for Beginners |
| Reusable Method | O(n²) | O(1) | Production Ready |
| Palindrome Pyramid | O(n²) | O(1) | Interview Favorite |
| Hollow Number Pyramid | O(n²) | O(1) | Advanced |
| Continuous Number Pyramid | O(n²) | O(1) | Frequently Asked |
Common Mistakes
Mistake 1
Printing an incorrect number of values.
Wrong
row
Correct
2 * row - 1
Mistake 2
Using the column number instead of the row number.
Wrong
System.out.print(col);
Correct
System.out.print(row);
Mistake 3
Incorrect leading spaces.
Always print
rows - row
leading spaces.
Mistake 4
Printing spaces after every row instead of before the numbers.
The alignment should always be:
- Leading spaces
- Numbers
- New line
Mistake 5
Forgetting to print a new line.
Always end each row with:
System.out.println();
Interview Follow-up Questions
Q1. What is a Number Pyramid?
Q2. Why do we print 2 × row − 1 numbers?
Q3. How are leading spaces calculated?
Q4. What is the difference between a Number Pyramid and a Star Pyramid?
Q5. Can you print a Hollow Number Pyramid?
Q6. Can you print a Palindrome Number Pyramid?
Q7. Can you print continuous numbers?
Q8. What is the time complexity?
Q9. Can you print the pyramid using recursion?
Q10. Can you generate the pattern using methods?
Related Pattern Problems
- Full Pyramid
- Hollow Pyramid
- Diamond Pattern
- Hollow Diamond
- Butterfly Pattern
- Floyd's Triangle
- Pascal's Triangle
- Number Diamond
- Alphabet Pyramid
Key Takeaways
- A Number Pyramid is a centered triangular arrangement of numbers.
- Every row contains 2 × row − 1 values.
- Leading spaces decrease by one with each row.
- The simplest Number Pyramid prints the current row number repeatedly.
- The same loop structure can be reused for Palindrome, Hollow, and Continuous Number Pyramids.
- The algorithm runs in O(n²) time using O(1) extra space.
Frequently Asked Interview Questions
Q1. Why does every row contain 2 × row − 1 numbers?
A centered pyramid expands equally on both sides. Each new row adds one position to the left and one to the right, increasing the width by two.
Q2. Why are leading spaces required?
Leading spaces ensure the numbers remain centered. Without them, the pattern becomes left-aligned instead of pyramid-shaped.
Q3. Why does the simplest Number Pyramid print the row number repeatedly?
Using the row number demonstrates the relationship between the current row and the generated output. It is the easiest number-based variation and forms the basis for more advanced patterns.
Q4. Can this solution be extended?
Yes. The same structure can be modified to build:
- Palindrome Number Pyramid
- Hollow Number Pyramid
- Continuous Number Pyramid
- Pascal's Triangle
- Floyd's Triangle
- Number Diamond
Only the value-generation logic changes.
Q5. What is the biggest difference between a Number Pyramid and a Palindrome Number Pyramid?
| Number Pyramid | Palindrome Number Pyramid |
|---|---|
| Repeats the same number across the row. | Numbers increase and then decrease symmetrically. |
| Simple value generation. | Requires additional number calculations. |
| Good beginner problem. | Common advanced interview question. |
Interview Tip
If an interviewer asks:
"Print a Number Pyramid in Java."
Break the solution into three simple steps:
- Print leading spaces to center the pyramid.
- Print
2 × row − 1numbers. - Print the current row number for each position.
Once you solve the basic pattern, explain that the same nested loop structure can be reused for Palindrome Number Pyramid, Hollow Number Pyramid, Continuous Number Pyramid, and Pascal's Triangle by changing only the number-generation logic. This demonstrates a strong understanding of reusable algorithms rather than memorizing individual patterns.