popular pattern programs for coding Interview

popular pattern programs for coding Interview
popular pattern programs for coding Interview

Pattern 1

1 
1 
1 2 
1 2 3 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5

Code for pattern 1

Pattern 2

1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 3 
1 2 
1 
1 

Code for pattern 2

Pattern 3

0     
10    
010   
1010  
01010

Code for pattern 3

Pattern 4

5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5

Code for pattern 4

Pattern 5

1 
1 0 
1 2 3 
1 0 1 0 
1 2 3 4 5

Code for pattern 5

Pattern 6

10 
9 9 
8 8 8 
7 7 7 7 
6 6 6 6 6

Code for pattern 6

Pattern 7

O
P P
Q Q Q 
R R R R
S S S S S

Code for pattern 7

Pattern 8

1#

2# 3#

4# 5# 6#

7# 8# 9# 10#

Code for pattern 8

How to solve number pattern programs

Number patterns follow the same structure as star patterns: an outer loop for the rows and an inner loop for the values in each row. The difference is that the inner loop prints a changing value, such as the column number j or the row number i, instead of a fixed star.

A simple way to approach any pattern

  1. Count the rows. This tells you how many times the outer loop runs.
  2. For each row, count how many numbers are printed and write the rule (for example, row i prints i numbers).
  3. Decide what value is printed: the column number, the row number, a running counter or a mirrored value.
  4. Print the values with print and finish each row with a line break.
  5. Dry-run the code for the first two or three rows on paper before you run it.

Common mistakes

  • Starting loops at 0 when the pattern starts at 1 (or the other way around).
  • Using < where <= is needed, which cuts the last row or column.
  • Forgetting the line break, so the whole pattern prints on one line.
  • Reusing a counter variable without resetting it at the start of each row.

The patterns on this page have code in Java, C++ and Python. The logic is the same in every language, so choose the one you use in interviews and practice writing it from memory. All of these run in O(n²) time.

Leave a Reply