Filled and Hollow Rectangle
Basic Nested LoopsSolved Oct 24, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementThe task is to print two fundamental patterns using nested loops, given dimensions N (rows) and M (columns):ReadHide
Problem Description
The task is to print two fundamental patterns using nested loops, given dimensions N (rows) and M (columns):
- Filled Rectangle: Print a solid block of characters (e.g.,
*). - Hollow Rectangle: Print a character only on the boundary (first/last row/column).
Examples
1. Filled Rectangle (N=3, M=5)
2. Hollow Rectangle (N=4, M=5)
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use two nested loops (i for rows, j for columns). For Hollow, add a conditional check for boundaries.
Approaches
1. Filled Rectangle (Optimal) - O(N · M) ⭐
Idea: The complexity is proportional to the area. Use one loop for rows (i) and one loop for columns (j).
2. Hollow Rectangle (Optimal) - O(N · M)
Idea: Requires a conditional check inside the inner loop: print the character only if the current position is on the boundary.
The condition is: i == 0 || i == N-1 || j == 0 || j == M-1
Solution code
#include <iostream>
using namespace std;
// Solves both Filled and Hollow Rectangle patterns
void solve_rectangles() {
int N, M; // N=Rows (Length L), M=Columns (Width W)
// Read N and M (For both patterns)
if (!(cin >> N >> M)) return;
// --- 1. Filled Rectangle (Example: N=3, M=5) ---
cout << "--- Filled Rectangle (" << N << "x" << M << ") ---\n";
for (int i = 0; i < N; ++i) { // Outer loop for N rows
for (int j = 0; j < M; ++j) { // Inner loop for M columns
cout << "*";
}
cout << "\n";
}
// --- 2. Hollow Rectangle (Example: N=4, M=5) ---
cout << "\n--- Hollow Rectangle (" << N << "x" << M << ") ---\n";
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
// Print '*' only if it's on the boundary (first/last row/column)
if (i == 0 || i == N - 1 || j == 0 || j == M - 1) {
cout << "*";
} else {
cout << " "; // Print space inside
}
}
cout << "\n";
}
}
int main() {
// Note: To run both, you would need to feed input N M N' M' for the second pattern.
// Assuming single input N M for simplicity, but the functions are complete.
solve_rectangles();
return 0;
}Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language