Pyramid · Pattern

Butterfly

Butterfly

Pyramid

Symbol pattern, solid style, size 5. Changing the rubric, style or size above re-shapes the loops — the structure stays identical while only the printed value or the cell filter changes.

Butterfly · output

Symbol · Solid · size 5

max 10
          
          
          
          
          
          
          
          
          
          
1 / 22
Drawing… ready to start.
Python · Butterfly · Python
    n = 5
    for i in range(2 * n):
        for j in range(2 * n):
            if (i < n and (j <= i or j >= 2 * n - 1 - i)) or (i >= n and (j <= 2 * n - 1 - i or j >= i)):
                print('*', end='')
            else:
                print(' ', end='')
        print()

Behind the Scenes Execution

registers · line tracking · call stack · memory regions
PC0x4000SP0x7FE8FLAGSZF 0OPenter function
Call Stack
main()@0x7FD0
n = 5i = -1j = -1count = 0
Memory Map
AddressVariableValue
Stack (4)
0x200n5
0x204i-1
0x208j-1
0x20Ccount0
String Pool (static) (2)
0x400000pool["*"]*
0x400008pool["\n"]\n
Heap (10)
0x3000grid[0]
0x3020grid[1]
0x3040grid[2]
0x3060grid[3]
0x3080grid[4]
0x30A0grid[5]
0x30C0grid[6]
0x30E0grid[7]
0x3100grid[8]
0x3120grid[9]
1 / 22
Pyramid

Butterfly

Two triangles facing each other with an empty gap that narrows and widens again: on every row the left wing grows while the right wing mirrors it, and the wings swap behaviour at the middle row — a butterfly. The row width formula min(i, 2n - 1 - i) + 1 drives both wings.

The size-5 grid

Ten rows across 10 columns with two wings: wing width climbs 1 to 5 then falls back, the left wing spans columns 0 to w - 1 and the right wing spans columns 10 - w to 9, and the gap between them is 10 - 2w spaces. The two middle rows have their wings touching.

Off-by-one traps

A gap of 2(5 - w) + 1 fuses the wings one row too early and clips the right edge on the widest rows; the wings and the gap must always total 10 columns.

Step by step

  1. Type n when the program asks — each wing is n rows tall and the body has 2n rows total.
  2. Loop i = 1 to n (upper half): print i values flush left, then pad the middle with 2·(n − i) spaces, then print i values again.
  3. Newline after each row — the gap closes as i approaches n.
  4. Loop i = n - 1 down to 1 (lower half): repeat exactly the same row recipe, so the wings shrink back.

Complexity

BestO(n²)
AverageO(n²)
WorstO(n²)
SpaceO(1) if printed directly · O(n²) if stored

When to use it

  • Mirrored blocks on one line
  • Combining grow and shrink phases in a single loop
Study tip: pick a size, then run and step through the execution. Watch the grid[r] rows in the Heap region and the i / j / count registers on the Stack — every printed character is one write to the grid.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits