The alphabet rangoli is a well-known HackerRank problem: draw a rangoli, the decorative floor design from India, out of letters and dashes. For size 3 the letters run c, b, a towards the centre and back out again.
Quick answer: Loop over rows r from β(n β 1) to n β 1. Row r has d = n β 1 β |r| letters on each side of its middle letter. Build the letters from the n-th letter down towards the middle and back, join them with dashes, and centre the row in a width of 4n β 3 with dashes.
This program is part of the String Patterns collection.
Table of contents8 sections
Alphabet Rangoli Pattern output
Output (size 3):
----c---- --c-b-c-- c-b-a-b-c --c-b-c-- ----c----

How the alphabet rangoli pattern works
The rangoli is a diamond in which each row is a palindrome of letters. Counting the rows from β(n β 1) to n β 1 makes the symmetry easy: the absolute value |r| is the distance from the middle row, so the top and bottom halves share one formula. Inside a row, the letter at offset t from the centre is 'a' + n β 1 β d + |t|: the centre letter is the smallest and the letters grow outwards. Every row is 4n β 3 characters wide, so the dashes on each side make up the difference.
- Set
width = 4n β 3. - For
rfrom1 β nton β 1, setd = n β 1 β |r|. - Build the letters for
tfromβdtod:'a' + n β 1 β d + |t|, joined with dashes. - Pad both sides with dashes to the full width and print the row.
Key takeaways
- Counting rows from
β(n β 1)lets one formula draw both halves. - Each row is a palindrome with the smallest letter in the middle.
- Every row is padded with dashes to
4n β 3characters. - Python’s
str.center(width, "-")does the padding in one call.
Alphabet Rangoli Pattern in C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n = 3;
int width = 4 * n - 3;
for (int r = 1 - n; r <= n - 1; r++) {
int d = n - 1 - abs(r); /* letters on each side of the middle one */
int pad = (width - (4 * d + 1)) / 2;
for (int k = 0; k < pad; k++)
printf("-");
for (int t = -d; t <= d; t++) {
printf("%c", 'a' + n - 1 - d + abs(t));
if (t < d)
printf("-");
}
for (int k = 0; k < pad; k++)
printf("-");
printf("\n");
}
return 0;
}Alphabet Rangoli Pattern in C++
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
int n = 3;
int width = 4 * n - 3;
for (int r = 1 - n; r <= n - 1; r++) {
int d = n - 1 - abs(r); // letters on each side of the middle one
int pad = (width - (4 * d + 1)) / 2;
cout << string(pad, '-');
for (int t = -d; t <= d; t++) {
cout << char('a' + n - 1 - d + abs(t));
if (t < d)
cout << "-";
}
cout << string(pad, '-') << endl;
}
return 0;
}Alphabet Rangoli Pattern in Java
public class AlphabetRangoli {
public static void main(String[] args) {
int n = 3;
int width = 4 * n - 3;
for (int r = 1 - n; r <= n - 1; r++) {
int d = n - 1 - Math.abs(r); // letters on each side of the middle one
int pad = (width - (4 * d + 1)) / 2;
StringBuilder row = new StringBuilder("-".repeat(pad));
for (int t = -d; t <= d; t++) {
row.append((char) ('a' + n - 1 - d + Math.abs(t)));
if (t < d)
row.append("-");
}
row.append("-".repeat(pad));
System.out.println(row);
}
}
}Alphabet Rangoli Pattern in Python
n = 3
width = 4 * n - 3
for r in range(1 - n, n):
d = n - 1 - abs(r) # letters on each side of the middle one
letters = [chr(ord("a") + n - 1 - d + abs(t)) for t in range(-d, d + 1)]
print("-".join(letters).center(width, "-"))Time and space complexity
The pattern has 2n β 1 rows of 4n β 3 characters, so it runs in O(nΒ²) time. The Python version builds one row at a time, which needs O(n) memory.
Frequently asked questions
What does the size-5 rangoli look like?
It has 9 rows and is 17 characters wide. The middle row is e-d-c-b-a-b-c-d-e, and the first and last rows are ——–e——–.
Why is the width 4n β 3?
The middle row has 2n β 1 letters and 2n β 2 dashes between them, which adds up to 4n β 3. Every other row is padded to the same width.
Is this pattern asked outside HackerRank?
Yes, it appears in Python practice sets and placement tests, often with the same name. The idea of counting rows from a negative number is useful for any diamond, such as the diamond star pattern.






