Java Interview Question: Reverse Pyramid Pattern with Time Complexity Analysis

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

  1. The first row should contain N stars separated by spaces.
  2. Each subsequent row should:
    • Have one additional leading space.
    • Have one less star than the previous row.
  3. The output should visually resemble a reverse pyramid.

Understanding the Pattern

Let’s analyze the output for N = 5.

RowLeading SpacesNumber of Stars
105
214
323
432
541

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

  1. Read input integer N.
  2. Iterate from row 0 to N-1.
  3. Print row number of leading spaces.
  4. Print (N-row) stars separated by spaces.
  5. Move to the next line.
  6. 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 row times.
  • 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

Leave a Reply

Your email address will not be published. Required fields are marked *