Diamond Kata
Level: Intermediate 30–60 minConcepts: 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
- The first and last rows contain a single
A - All rows except the first and last have exactly two identical letters
- The diamond is vertically and horizontally symmetric
- The width equals the height
- The top half is a mirror of the bottom half
- Leading spaces align the letters into a diamond shape
- Inner spaces between the two letters increase by 2 for each row from the top
Test Cases
| Input | Height | Width | First Row | Middle Row |
|---|---|---|---|---|
A | 1 | 1 | A | A |
B | 3 | 3 | ·A | B·B |
C | 5 | 5 | ··A | C···C |
E | 9 | 9 | ····A | E·······E |
(Dots represent spaces for clarity)
Bonus
- Accept lowercase input and normalize to uppercase
- Add a
hollowoption 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.