New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Door Mat Pattern (HackerRank Designer Door Mat)

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
  1. Door Mat Pattern output
  2. How the door mat pattern works
  3. Door Mat Pattern in C
  4. Door Mat Pattern in C++
  5. Door Mat Pattern in Java
  6. Door Mat Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

Door Mat Pattern output

Output (7 Γ— 21):

---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
Door Mat Pattern layout diagram, row by row (7 Γ— 21)
How each row of the door mat pattern is built (7 Γ— 21). Blue cells are printed characters; grey dots are spaces.

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.

  1. Set m = 3n, the width of the mat.
  2. For each row i: if it is the middle row, centre WELCOME in m columns.
  3. Otherwise set k = i above the middle or n βˆ’ i + 1 below it, and print 2k βˆ’ 1 units of .|. centred with dashes.
  4. 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 i above the middle has 2i βˆ’ 1 units.
  • 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 7 of 17 in Symbol Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.