Pyramid · Pattern

Diamond

Diamond

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.

Diamond · output

Symbol · Solid · size 5

max 9
         
         
         
         
         
         
         
         
         
1 / 20
Drawing… ready to start.
Python · Diamond · Python
n = int(input("Enter number of rows: "))
# Upper half — pyramid grows to its widest row
for i in range(1, n + 1):
    print((n - i) * " ", end='')
    for j in range(1, 2 * i - 1 + 1):
        print('*', end='')
    print()

# Lower half — pyramid shrinks back to a tip
for i in range(n - 1, 1 - 1, -1):
    print((n - i) * " ", end='')
    for j in range(1, 2 * i - 1 + 1):
        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 (9)
0x3000grid[0]
0x3020grid[1]
0x3040grid[2]
0x3060grid[3]
0x3080grid[4]
0x30A0grid[5]
0x30C0grid[6]
0x30E0grid[7]
0x3100grid[8]
1 / 20
Pyramid

Diamond

A diamond is two triangles glued together: an expanding pyramid on top and a shrinking one below. The trick is to derive the row width from the distance to the middle row using the absolute value abs(i - (n - 1)).

The size-5 grid

Nine rows in a 9-column frame: the upper half grows 1, 3, 5, 7, 9 with margin 4 - r, then the lower half repeats those widths in reverse, giving 1, 3, 5, 7, 9, 7, 5, 3, 1. The widest row is printed once, in the middle.

Off-by-one traps

Printing the upper half for r = 0 to 4 and then the lower half from 5 down to 1 duplicates the 9-wide row and adds a tenth row; the second loop must start at 4, not at 5.

Step by step

  1. Type n when the program asks.
  2. Section “// Upper half”: loop i = 1 to n.
  3. Print n - i spaces, then loop j = 1 to (2*i - 1) and print a symbol — the pyramid grows.
  4. Print a newline after the row.
  5. Section “// Lower half”: loop i = n - 1 down to 1, same padding and width — the diamond closes back to a tip.

Complexity

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

When to use it

  • The abs() centre-distance trick
  • Piecewise mirroring shapes
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