The door mat pattern comes from HackerRank’s “Designer Door Mat” problem: a mat n rows tall and 3n columns wide, with a design of .|. units, the word WELCOME in the middle, and dashes filling the rest.
Quick answer: Each row above the middle has 2i β 1 units of .|., centred in 3n columns with dashes. The middle row is WELCOME centred the same way, and the rows below mirror the rows above. With n = 7 the mat is 7 Γ 21.
This program is part of the Symbol Patterns collection.
Table of contents8 sections
Door Mat Pattern output
Output (7 Γ 21):
---------.|.--------- ------.|..|..|.------ ---.|..|..|..|..|.--- -------WELCOME------- ---.|..|..|..|..|.--- ------.|..|..|.------ ---------.|.---------

How the door mat pattern works
Every unit .|. is three characters, which is why the mat is 3n wide: the widest row, just above or below the middle, holds n β 2 units and the dashes on each side. Each row is centred, so the dashes on both sides are (3n β 3 Γ units) / 2. The rows below the middle use n β i + 1 instead of i, which mirrors the top half.
- Set
m = 3n, the width of the mat. - For each row
i: if it is the middle row, centre WELCOME inmcolumns. - Otherwise set
k = iabove the middle orn β i + 1below it, and print2k β 1units of.|.centred with dashes. - Move to the next line.
Key takeaways
- The mat is three times as wide as it is tall, because each unit is three characters.
- Row
iabove the middle has2i β 1units. - Dashes on both sides centre every row.
- Python’s
str.center(m, "-")does all the padding in one call.
Door Mat Pattern in C
#include <stdio.h>
int main(void) {
int n = 7; /* rows: an odd number */
int m = 3 * n; /* columns */
for (int i = 1; i <= n; i++) {
if (i == n / 2 + 1) { /* middle row */
int pad = (m - 7) / 2;
for (int c = 0; c < pad; c++)
printf("-");
printf("WELCOME");
for (int c = 0; c < pad; c++)
printf("-");
} else {
int k = (i <= n / 2) ? i : n - i + 1;
int units = 2 * k - 1; /* how many ".|." on this row */
int pad = (m - 3 * units) / 2;
for (int c = 0; c < pad; c++)
printf("-");
for (int u = 0; u < units; u++)
printf(".|.");
for (int c = 0; c < pad; c++)
printf("-");
}
printf("\n");
}
return 0;
}Door Mat Pattern in C++
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 7; // rows: an odd number
int m = 3 * n; // columns
for (int i = 1; i <= n; i++) {
if (i == n / 2 + 1) { // middle row
string pad((m - 7) / 2, '-');
cout << pad << "WELCOME" << pad << endl;
} else {
int k = (i <= n / 2) ? i : n - i + 1;
int units = 2 * k - 1; // how many ".|." on this row
string pad((m - 3 * units) / 2, '-'), middle;
for (int u = 0; u < units; u++)
middle += ".|.";
cout << pad << middle << pad << endl;
}
}
return 0;
}Door Mat Pattern in Java
public class DoorMat {
public static void main(String[] args) {
int n = 7; // rows: an odd number
int m = 3 * n; // columns
for (int i = 1; i <= n; i++) {
if (i == n / 2 + 1) { // middle row
String pad = "-".repeat((m - 7) / 2);
System.out.println(pad + "WELCOME" + pad);
} else {
int k = (i <= n / 2) ? i : n - i + 1;
int units = 2 * k - 1; // how many ".|." on this row
String pad = "-".repeat((m - 3 * units) / 2);
System.out.println(pad + ".|.".repeat(units) + pad);
}
}
}
}Door Mat Pattern in Python
n = 7 # rows: an odd number
m = 3 * n # columns
for i in range(1, n + 1):
if i == n // 2 + 1: # middle row
print("WELCOME".center(m, "-"))
else:
k = i if i <= n // 2 else n - i + 1
units = 2 * k - 1 # how many ".|." on this row
print((".|." * units).center(m, "-"))Time and space complexity
The mat has n rows of 3n characters, so the program runs in O(nΒ²) time. It needs O(1) extra memory in C and O(n) for the row strings in the other languages.
Frequently asked questions
Why must n be odd?
An odd number of rows gives a single middle row for WELCOME, with the same number of design rows above and below it. HackerRank’s problem states this as part of the input.
How is the padding worked out?
A row with u units is 3u characters wide. The remaining 3n β 3u characters are split evenly, so each side gets (3n β 3u) / 2 dashes.
Can I change the word in the middle?
Yes, any word works as long as it fits in 3n columns. The padding formula uses the word’s length, so replace 7 with the new length.






