Diamond Kata

Level: Intermediate 30–60 min

Concepts: AlgorithmsStringsIncremental Design


Given a letter, generate a diamond shape that starts with A, widens to the given letter, then narrows back to A.

Requirements

Given A, return:

A

Given B, return:

 A
B B
 A

Given C, return:

  A
 B B
C   C
 B B
  A

Given D, return:

   A
  B B
 C   C
D     D
 C   C
  B B
   A

Rules

  1. The first and last rows contain a single A
  2. All rows except the first and last have exactly two identical letters
  3. The diamond is vertically and horizontally symmetric
  4. The width equals the height
  5. The top half is a mirror of the bottom half
  6. Leading spaces align the letters into a diamond shape
  7. Inner spaces between the two letters increase by 2 for each row from the top

Test Cases

InputHeightWidthFirst RowMiddle Row
A11AA
B33·AB·B
C55··AC···C
E99····AE·······E

(Dots represent spaces for clarity)

Bonus

  • Accept lowercase input and normalize to uppercase
  • Add a hollow option that only prints the border characters and fills the interior with spaces
  • Validate input is a single letter A-Z

Hint

Don’t try to build the whole diamond at once. Focus on generating a single row first — given a target letter and a current letter, produce the correct row with the right leading spaces and inner spaces. Once a single row works, the diamond is just a sequence of rows mirrored vertically.