VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-print-hollow-rectangle-or-square-star-patterns/

⇱ Print Hollow Rectangle Pattern - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print Hollow Rectangle Pattern

Last Updated : 22 May, 2026

Given two integers n and m, print a hollow rectangle star pattern of the given n rows and m columns. In this pattern, stars (*) are printed on the boundary of the rectangle, while the inner area contains spaces.

Example:

Input: n = 6, m = 20
Output:

👁 420851436
Hollow Rectangle

Using Nested Loops – O(n*m) Time and O(1) Space

The pattern can be printed using two nested loops. The outer loop iterates through the rows, and the inner loop iterates through the columns. A star (*) is printed when the current position is on the first row, last row, first column, or last column; otherwise, a space is printed.

Comment