Foundations · Nested loops
Pattern printing, derived not guessed
Every star pattern is the same two questions asked once per row: how many spaces, then how many characters. Derive both from the row number and you can print any shape without counting by eye.
Print a pyramid one character at a time →01 The idea
Two loops, one question per row
A pattern is a picture made of characters. Your program cannot draw it. It can only print one character at a time, left to right and top to bottom, and it can never go back. So you break the picture into rows, and for each row you answer one question: which characters, in what order, and how many of each. The outer loop walks the rows. The inner loop prints the characters of the row it is standing on.
Everything hard about pattern printing is deciding how many times that inner loop should run. That count is always a formula in the row number. So do not guess the spaces: draw the pattern on paper, number the rows, count the characters on each row, and read the formula off the counts.
02 Worked example
A 5-row pyramid, traced row by row
Take n = 5 and derive the pyramid one row at a time. A dot marks a space the program actually prints; a green cell is a star. Row i needs n − i spaces and 2i − 1 stars — check both formulas on every row before you trust them.
Read the two columns of numbers on their own. Spaces go 4, 3, 2, 1, 0 and stars go 1, 3, 5, 7, 9. Those two sequences are the entire program, and the totals fall straight out of them: 4+3+2+1+0 = 10 spaces and 1+3+5+7+9 = 25 stars, so 35 characters for a 5-row pyramid. Every pattern you will be asked for is a different pair of sequences, derived exactly this way.
03 Mechanics
From five typed strings to two formulas
The version on the left is what most people write first: count the spaces by eye, type each line. It is correct, and it is a dead end — it prints a 5-row pyramid and nothing else. The version on the right prints the same picture, but every count comes from i, so the same six lines print 3 rows or 20.
Naive — one string per row
print " *"print " ***"print " *****"print " *******"print "*********"
Derived — counts from the row index
for i = 1 to n: spaces = n - i stars = 2*i - 1 repeat spaces times: print " " repeat stars times: print "*" print newline
The counts are named before anything prints. spaces and stars exist as values you can compare against the trace in section 02. A wrong pyramid then costs you one arithmetic error to find instead of five string literals to re-count by eye.
n became an input. The typed version is welded to five rows. The follow-up question is always "now do it for n = 7", and the derived version already does — the picture never appears in the code, only the rule that generates it.
Spaces are emitted before stars. Output moves left to right and cannot go back, so the padding has to exist before the thing it pads. Swap lines 4 and 5 and you print a left-aligned staircase with trailing blanks, not a pyramid.
05 Cheat sheet
Five shapes worth memorising
| Pattern (n rows, i from 1) | Row i prints | Total stars |
|---|---|---|
| Solid square | n stars | n² |
| Right triangle | i stars | n(n+1)/2 |
| Inverted right triangle | n − i + 1 stars | n(n+1)/2 |
| Pyramid | n − i spaces, 2i − 1 stars | n² |
| Hollow square | star only on the border | 4n − 4 (n ≥ 2) |
06 Where & why
You will not ship a pyramid
Nested-loop printing is not an algorithm you will put into production. Its value is that it forces you to turn a picture into arithmetic, which is the same move every grid, matrix and DP-table problem asks for later.
Reach for the derive-then-print method when…
Use something else when…
| Situation | Better choice | Why |
|---|---|---|
| The shape depends on data, not on the row number | Fill a 2-D array, print it at the end | An array lets you write cells in any order. Direct printing can only go left to right, top to bottom, and never revisit a cell. |
| The output goes to a file, a UI or a log | Build each row as a string, then emit | Direct printing costs one write per character — about n² writes. Building rows costs n writes and one string per row of O(n) space. |
| Rows are irregular, like a calendar or a report | Padding and alignment helpers | Left-pad and centre functions already handle width. Hand-counted spaces break the moment a value's width changes. |
| The pattern is a fractal, like Sierpinski's triangle | Recursion over the grid | There is no simple per-row count to derive. The shape is defined in terms of smaller copies of itself, so the code has to be too. |
07 Interview questions
What they actually ask
Pattern questions open screening tests and language rounds. What is being marked is never the printed shape — it is whether you derived the counts or guessed at them.
What is a nested loop?
How do you decide what the inner loop bound should be?
What is the time complexity of printing an n-row pyramid?
Is the space complexity also quadratic?
Why must the spaces be printed before the stars?
Your loop starts at i = 0 instead of 1. What changes?
How would you print a hollow square instead of a solid one?
What is an inverted pyramid, and how does the formula change?
Right triangle versus pyramid — what is the actual difference in code?
Can you print a pyramid with a single loop?
Where would you actually use this?
08 Practice problems
Six to derive yourself
For each one: draw the shape, number the rows, and write the two counts down before you write a loop. If you cannot fill that table by hand, no amount of typing will fix it.