15 Pattern Printing Questions with Solutions and Explanations.
Pattern printing is a great way to improve logical thinking and problem-solving skills. Below are 15 unique pattern questions, their solutions, and step-by-step explanations.
My Interview Problem and Solution :-
A regional bank branch has implemented a digital queue monitoring system to evaluate customer service efficiency. The system stores historical wait times (in minutes) of previously served customers.
As part of their service optimization strategy, the manager wants to analyze the adjusted average wait time after accommodating a new customer.
Your task is to write a program that:
-
Accepts a comma-separated string of integer values representing past customer wait times.
-
Accepts an additional integer input denoting the wait time estimate for a newly arriving customer.
-
Updates the queue by appending this new time.
-
Calculates the rounded average wait time for all customers in the queue.
Constraints:
-
All input wait times are in minutes and positive integers.
-
You must round the final average to the nearest integer using standard rounding.
Example:
Input:12,18,24,30
20
Output:21
(Explanation: Updated list = [12,18,24,30,20] → Avg = 20.8 → Rounded = 21)
Solution :-
def calc(queue, new) : # Function Definition return round(sum(queue)/len(queue)) # Returns Avg Rounder Value queue = list(map(int, input().split(","))) # Queue of Wait times new = int(input()) # New Customer queue.append(new) # Adding New Customer to Queue print(calc(queue, new))
1️⃣ Right-Angled Triangle Pattern
Problem Statement:
Print the following pattern for N = 5:
*
**
***
****
*****
Solution:
n = 5 for i in range(1, n+1): print('*' *
i)
Explanation:
- Loop runs from 1 to N.
'*' * i
printsi
stars in each row.
2️⃣ Inverted Right-Angled Triangle
Problem Statement:
Print the following pattern for N = 5:
*****
****
***
**
*
Solution:
n = 5
for i in range(n, 0, -1):
print('*' * i)
Explanation:
- Loop runs in reverse order from N to 1.
- Each row has
i
stars.
3️⃣ Pyramid Pattern
Problem Statement:
Print the following pyramid pattern for N = 5:
*
***
*****
*******
*********
Solution:
n = 5
for i in range(n):
print(' ' * (n-i-1) + '*' * (2*i+1))
Explanation:
- Spaces decrease while stars increase in each row.
2*i+1
ensures odd numbers of stars per row.
4️⃣ Diamond Pattern
Problem Statement:
Print the following diamond pattern for N = 5:
*
***
*****
*******
*********
*******
*****
***
*
Solution:
def print_diamond(n):
for i in range(n):
print(' ' * (n-i-1) + '*' * (2*i+1))
for i in range(n-2, -1, -1):
print(' ' * (n-i-1) + '*' * (2*i+1))
print_diamond(5)
Explanation:
- First loop prints a pyramid.
- Second loop prints an inverted pyramid.
5️⃣ Hollow Square Pattern
Problem Statement:
Print a hollow square for N = 5:
*****
* *
* *
* *
*****
Solution:
n = 5
for i in range(n):
if i == 0 or i == n-1:
print('*' * n)
else:
print('*' + ' ' * (n-2) + '*')
Explanation:
- First and last row print
N
stars. - Middle rows print stars at both ends.
6️⃣ Zig-Zag Pattern
Problem Statement:
Print a Zig-Zag pattern for N = 5:
* * *
* * * * *
* * *
Solution:
n = 5
for i in range(3):
for j in range(n):
if (i + j) % 4 == 0 or (i == 1 and j % 4 == 2):
print('*', end=' ')
else:
print(' ', end=' ')
print()
Explanation:
- Conditions are used to place stars at specific locations.
7️⃣ Hollow Triangle
Problem Statement:
Print the following hollow triangle pattern for N = 5:
*
* *
* *
* *
*********
Solution:
n = 5
for i in range(n):
spaces = ' ' * (n - i - 1)
if i == 0:
stars = '*'
elif i == n - 1:
stars = '*' * (2 * i + 1)
else:
stars = '*' + ' ' * (2 * i - 1) + '*'
print(spaces + stars)
Eplanation:
- The first row has a single star.
- The last row is filled completely.
- Intermediate rows have spaces in between.
8️⃣ Number Pyramid
Problem Statement:
Print a number pyramid for N = 5:
1
121
12321
1234321
123454321
Solution:
n = 5
for i in range(1, n+1):
print(' ' * (n-i), end='')
for j in range(1, i+1):
print(j, end='')
for j in range(i-1, 0, -1):
print(j, end='')
print()
Explanation:
- Each row prints numbers increasing, then decreasing.
- Spaces are used for alignment.
9️⃣ Hollow Diamond Pattern
Problem Statement:
Print the following hollow diamond pattern for N = 5:
*
* *
* *
* *
*********
* *
* *
* *
*
Solution:
n = 5
# Upper part of the pattern
for i in range(n):
spaces = ' ' * (n - i - 1)
if i == 0:
print(spaces + '*')
else:
inner_spaces = ' ' * (2 * i - 1)
print(spaces + '*' + inner_spaces + '*')
# Lower part of the pattern
for i in range(n - 2, -1, -1):
spaces = ' ' * (n - i - 1)
if i == 0:
print(spaces + '*')
else:
inner_spaces = ' ' * (2 * i - 1)
print(spaces + '*' + inner_spaces + '*')
Explanation:
- Top half follows increasing spaces with two stars on the edges.
- The middle row is fully filled.
- The bottom half mirrors the top half.
🔟 Butterfly Pattern
Problem Statement:
Print the following butterfly pattern for N = 4:
* *
** **
*** ***
********
*** ***
** **
* *
Solution:
n = 4
# Upper part of the pattern
for i in range(1, n + 1):
left_stars = '*' * i
spaces = ' ' * (2 * (n - i))
right_stars = '*' * i
print(left_stars + spaces + right_stars)
# Lower part of the pattern
for i in range(n - 1, 0, -1):
left_stars = '*' * i
spaces = ' ' * (2 * (n - i))
right_stars = '*' * i
print(left_stars + spaces + right_stars)
Explanation:
- The first half prints increasing stars, spaces, and then stars again.
- The second half is the reverse of the first half.
1️⃣1️⃣ Pascal’s Triangle
Problem Statement:
Print Pascal’s Triangle for N = 5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Solution:
def pascal(n):
for i in range(n):
num = 1
print(' ' * (n - i), end='')
for j in range(i + 1):
print(num, end=' ')
num = num * (i - j) // (j + 1)
print()
pascal(5)
Explanation:
- Uses binomial coefficients to calculate values.
- The formula
num = num * (i - j) // (j + 1)
computes the next number in the row.
1️⃣2️⃣ X Pattern
Problem Statement:
Print an X pattern for N = 5:
* *
* *
*
* *
* *
Solution:
n = 5
for i in range(n):
for j in range(n):
if i == j or i + j == n - 1:
print('*', end='')
else:
print(' ', end='')
print()
Explanation:
- Stars appear where
i == j
(diagonal) ori + j == n - 1
(anti-diagonal).
1️⃣3️⃣ Hourglass Pattern
Problem Statement:
Print the following hourglass pattern for N = 5:
*********
* *
* *
* *
*
* *
* *
* *
*********
Solution:
n = 5
# Upper part of the pattern
for i in range(n):
leading_spaces = ' ' * i
middle_spaces = ' ' * (2 * (n - i - 1) - 1)
right_star = '*' if i != n - 1 else ''
print(leading_spaces + '*' + middle_spaces + right_star)
# Lower part of the pattern
for i in range(n - 2, -1, -1):
leading_spaces = ' ' * i
middle_spaces = ' ' * (2 * (n - i - 1) - 1)
right_star = '*' if i != n - 1 else ''
print(leading_spaces + '*' + middle_spaces + right_star)
Explanation:
- The pattern is similar to a diamond, but inverted.
1️⃣4️⃣ Palindromic Number Pyramid
Problem Statement:
Print the palindromic number pyramid for N = 5:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
Solution:
n = 5
for i in range(1, n + 1):
print(' ' * (n - i), end='')
for j in range(i, 0, -1):
print(j, end=' ')
for j in range(2, i + 1):
print(j, end=' ')
print()
Explanation:
- First, numbers decrease from
i
to1
. - Then, numbers increase from
2
toi
. - Spaces are used to align the pyramid.
1️⃣5️⃣ Hollow Pyramid Pattern
Problem Statement:
Print the following hollow pyramid pattern for N = 5:
*
* *
* *
* *
*********
Solution:
n = 5
for i in range(n):
leading_spaces = ' ' * (n - i - 1)
middle_spaces = ' ' * (2 * i - 1)
right_star = '*' if i > 0 else ''
if i == n - 1:
print('*' * (2 * i + 1))
else:
print(leading_spaces + '*' + middle_spaces + right_star)
Explanation:
- Top rows have stars at the edges and spaces in between.
- Last row is completely filled with stars
10 thoughts on “Top 15 Pattern Programs For Logic Building”