Why interviewers still ask this
Pattern programs don't test system design or optimization. What they test is whether a candidate can translate a 2D shape into row and column bounds without hesitating, and whether they can debug an off-by-one error out loud when the shape comes out wrong. For entry-level hiring at scale, that's a fast, cheap signal: a candidate who freezes on a triangle loop is likely to freeze on harder nested logic later.
That's a narrower kind of signal than you'll see in our Top Python Coding Interview Questions guide, where most questions test problem-solving and idiom. Here, most questions test loop mechanics. A few, flagged below, go further and test real algorithmic thinking (Pascal's Triangle is the standout).
We've tiered this guide accordingly: the first 3 patterns get full treatment because they establish the core mental model everything else builds on. The remaining 7 are tighter: one working example and the one insight that matters, labeled Logic: rather than Evaluates:, because for most of these there isn't a deeper interviewer-psychology story to tell beyond "can you construct nested loops correctly."
A quick Python-specific note before the patterns: print() adds a newline by default, so building a row across multiple print calls needs end="" to keep everything on one line. Every example below uses that.
1. Right-Angled Triangle Pattern
The most commonly asked pattern program, and usually the first one in any placement drive.
*
* *
* * *
* * * *
* * * * *n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print("*", end=" ")
print()Common mistake:
# Wrong: inner loop bound doesn't depend on the outer loop
for i in range(1, n + 1):
for j in range(1, n + 1): # should be range(1, i + 1)
print("*", end=" ")
print()This prints a full square, not a triangle. The bug is subtle because the code runs without errors; it just prints the wrong shape. It's a good example of why "does it run" and "is it correct" are different questions, even in three lines of code.
Evaluates: whether the candidate understands that the inner loop's bound must be a function of the outer loop's current value. This is the core idea every other pattern builds on.
2. Pyramid (Centered Triangle) Pattern
Introduces a second variable: leading spaces that shrink as the row grows.
*
* *
* * *
* * * *
* * * * *n = 5
for i in range(1, n + 1):
print(" " * (n - i), end="")
for j in range(1, i + 1):
print("* ", end="")
print()Common mistake:
# Wrong: space count doesn't shrink with row number
for i in range(1, n + 1):
print(" " * n, end="") # should be n - i
...Interviewers often ask candidates to trace this by hand for n = 3 before typing anything. If you can't predict the space count for row 2 on paper, the code won't come out right either.
Evaluates: whether the candidate can manage two independent counters (spaces and stars) inside the same row without conflating them. This is the first real test of composing two loop conditions. Using " " * (n - i) instead of a manual space loop is also a small idiom signal, since Python string multiplication is the natural tool here.
3. Diamond Pattern
A diamond is a pyramid followed by an inverted pyramid. This is usually where interviewers check whether a candidate can reuse logic instead of writing everything from scratch.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*n = 5
# upper half (pyramid, odd star counts)
for i in range(1, n + 1):
print(" " * (n - i), end="")
print("* " * (2 * i - 1))
# lower half (inverted pyramid)
for i in range(n - 1, 0, -1):
print(" " * (n - i), end="")
print("* " * (2 * i - 1))Common mistake: candidates often get the star count formula backwards, using 2 * i instead of 2 * i - 1, which produces an even number of stars per row and breaks the symmetry needed for a proper diamond point.
Evaluates: whether the candidate can decompose a compound shape into two simpler shapes they've already solved, and reason about how the row/column relationship flips for the second half. It's a genuine "can you build on your own logic" signal, not just syntax recall. Using "* " * n instead of a manual inner loop is also a natural idiom check in Python specifically.
4. Inverted Triangle Pattern
* * * * *
* * * *
* * *
* *
*n = 5
for i in range(n, 0, -1):
for j in range(1, i + 1):
print("*", end=" ")
print()Logic: run the outer loop backwards from n to 1 using range(n, 0, -1), and reuse the same inner-loop bound as the right-angled triangle. The shape flips just by reversing the outer loop direction.
5. Number Pyramid Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()Logic: identical structure to the right-angled triangle. The only change is printing the loop variable j instead of a fixed character. Interviewers sometimes ask this right after Pattern 1 specifically to see if the candidate notices the reuse.
6. Floyd's Triangle
1
2 3
4 5 6
7 8 9 10n = 4
num = 1
for i in range(1, n + 1):
for j in range(1, i + 1):
print(num, end=" ")
num += 1
print()Logic: same nested-loop skeleton as the number pyramid, but the counter lives outside both loops and increments continuously instead of resetting each row. This is a small but common trip-up for candidates who initialize num inside the outer loop by mistake.
7. Pascal's Triangle
Unlike the rest of this list, Pascal's Triangle isn't just a loop-printing exercise. It requires computing binomial coefficients, which makes it a legitimate small algorithms question.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1n = 5
for i in range(n):
val = 1
row = []
for j in range(i + 1):
row.append(val)
val = val * (i - j) // (j + 1)
print(" ".join(str(x) for x in row))Evaluates: whether the candidate knows (or can derive) that each entry is C(i, j), and can compute it iteratively without recalculating factorials from scratch each time. This is the one pattern question that tests actual algorithmic reasoning rather than loop bookkeeping.
8. Hollow Square / Rectangle Pattern
* * * * *
* *
* *
* *
* * * * *n = 5
for i in range(1, n + 1):
for j in range(1, n + 1):
if i == 1 or i == n or j == 1 or j == n:
print("*", end=" ")
else:
print(" ", end=" ")
print()Logic: print a star only on the border rows/columns (i == 1, i == n, j == 1, j == n) and a blank space everywhere else. This is the first pattern that needs a conditional inside the loop instead of just loop bounds, a useful checkpoint for whether a candidate over-complicates it.
9. Butterfly Pattern
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *n = 4
# upper half
for i in range(1, n + 1):
print("* " * i, end="")
print(" " * (2 * (n - i)), end="")
print("* " * i)
# lower half
for i in range(n, 0, -1):
print("* " * i, end="")
print(" " * (2 * (n - i)), end="")
print("* " * i)Logic: two mirrored triangles per row with a variable gap in between. It's mechanically similar to the diamond (Pattern 3), but with three sections per row instead of two, so it's mostly a test of whether the candidate can keep three counters straight without mixing them up.
10. Zigzag (Z) Pattern
* * * * *
*
*
*
* * * * *n = 5
for i in range(1, n + 1):
for j in range(1, n + 1):
if i == 1 or i == n or i + j == n + 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()Logic: the diagonal is the interesting part. Printing a star when i + j == n + 1 is the standard trick for anti-diagonals, worth remembering since it shows up again in matrix/2D-array questions beyond pattern programs.
How should you prepare for pattern-program interview questions?
- Practice tracing by hand before typing code. For any new shape, write out what row 1, row 2, and row 3 should look like on paper first. Most bugs in pattern programs come from guessing the loop bounds instead of deriving them.
- Learn the row/column relationship, not the code. Once you know that a right-angled triangle's inner bound is
i, a pyramid's space count isn - i, and a diamond is two pyramids stacked, you can rebuild any variation live in an interview, including shapes you've never seen before. - Remember Python's
printquirk.print()adds a newline by default, so every row that's built across multipleprintcalls needsend=""on the earlier ones. Forgetting this is the single most common Python-specific bug in this category, separate from the loop logic itself.
Frequently asked questions
Are pattern programs asked in product-company interviews? Rarely. They're far more common in campus placement drives and service-company screening rounds. Product companies typically ask array, string, or data-structure questions instead. See our Top Python Coding Interview Questions guide for the kind of questions asked at that level.
Do I need to memorize all pattern programs? No. Memorizing code is fragile because interviewers often ask for small variations (numbers instead of stars, a different size, a mirrored shape). It's more reliable to understand the row/column bound logic and derive the loop conditions live.
What's the most commonly asked pattern program? The right-angled triangle and the pyramid are the two most frequently asked, usually as warm-up questions before a more complex shape or a follow-up variation.
Can pattern programs be written using recursion instead of loops? Yes, though it's uncommon in interviews. Most interviewers expect an iterative (loop-based) solution since that's what's being tested: nested loop control, not recursion.
Is Pascal's Triangle really a "pattern program"? It's usually grouped with pattern programs because of its shape, but it's meaningfully different. It requires computing binomial coefficients rather than just managing loop bounds, so treat it as a small algorithms question if it comes up.
Related tutorials: Top Python Coding Interview Questions · Python String Manipulation Interview Questions · Python Data Structures Interview Questions