Part 5 of 5

Patterns & Interview Strategy

The final stretch! We'll cover visual pattern logic and, more importantly, the framework you need to solve ANY problem an interviewer throws at you.

Pattern Printing

Mastering nested loops and spatial logic.

Pyramid Pattern

What You'll Learn

Coordinating spaces and stars in nested loops.

Real-World Context

"Printing a classic triangle shape. It's all about calculating how many spaces vs how many stars per row."

Breaking It Down

  • Outer loop for rows (i from 1 to N)
  • Inner loop 1: Print N-i spaces
  • Inner loop 2: Print 2*i - 1 stars
  • Print a newline
Python 3
def print_pyramid(n):
    for i in range(1, n + 1):
        print(" " * (n - i) + "*" * (2 * i - 1))

Line-by-Line Walkthrough

The number of spaces decreases as we go down, while the number of stars increases. The formula 2*i - 1 ensures an odd number of stars, creating the centered pyramid look.

Complexity Analysis

  • Time: O(N²)
  • Space: O(1)
  • Nested loops to print each character.

Common Mistakes

  • Forgetting the spaces
    Without spaces, you'll just get a left-aligned triangle.

Try It Yourself

Can you print an inverted pyramid?

Floyd's Triangle

What You'll Learn

Using a running counter in nested loops.

Real-World Context

"A right-angled triangle where numbers increase sequentially. 1, then 2 3, then 4 5 6..."

Breaking It Down

  • Initialize a counter = 1
  • Outer loop for rows
  • Inner loop for columns (up to current row number)
  • Print counter and increment it
Python 3
def floyds_triangle(n):
    num = 1
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print(num, end=" ")
            num += 1
        print()

Line-by-Line Walkthrough

Unlike other patterns where the value depends on i or j, here we use a global counter that never resets.

Complexity Analysis

  • Time: O(N²)
  • Space: O(1)
  • We print N*(N+1)/2 numbers in total.

Common Mistakes

  • Resetting the counter inside the outer loop
    The counter should be initialized outside all loops to keep increasing.

Try It Yourself

Can you print the triangle using only even numbers?

Diamond Pattern

What You'll Learn

Combining normal and inverted pyramids.

Real-World Context

"Printing a diamond shape. It's essentially a pyramid followed by an inverted pyramid."

Breaking It Down

  • Print a pyramid of size N
  • Print an inverted pyramid of size N-1
Python 3
def print_diamond(n):
    # Upper part
    for i in range(1, n + 1):
        print(" " * (n - i) + "*" * (2 * i - 1))
    # Lower part
    for i in range(n - 1, 0, -1):
        print(" " * (n - i) + "*" * (2 * i - 1))

Line-by-Line Walkthrough

We reuse the pyramid logic. The second loop simply runs backwards to create the tapering effect.

Complexity Analysis

  • Time: O(N²)
  • Space: O(1)
  • Nested loops to print each character.

Common Mistakes

  • Starting the second loop from N
    If you start from N, the middle row will be printed twice. Start from N-1.

Try It Yourself

Can you print a hollow diamond?

Number Pattern

What You'll Learn

Using loop indices as values.

Real-World Context

"Printing a triangle of numbers, e.g., 1, 22, 333, 4444..."

Breaking It Down

  • Outer loop for rows (i from 1 to N)
  • Inner loop for columns (j from 1 to i)
  • Print the value of i
Python 3
def number_pattern(n):
    for i in range(1, n + 1):
        for j in range(i):
            print(i, end="")
        print()

Line-by-Line Walkthrough

The value printed is the current row number, and it's printed 'row number' times.

Complexity Analysis

  • Time: O(N²)
  • Space: O(1)
  • Nested loops to print each digit.

Common Mistakes

  • Printing j instead of i
    Printing j would give you 1, 12, 123, 1234...

Try It Yourself

Can you print the pattern 1, 121, 12321, 1234321?

The UMPIRE Method

When you face a problem you've never seen before, don't panic. Use this 6-step framework used by engineers at Google and Meta.

U

Understand

Ask clarifying questions. What are the inputs? What are the edge cases (empty, negative, large)?

M

Match

Does this look like a problem you've seen? Is it a string problem? Can I use two pointers or a hash map?

P

Plan

Write down the steps in plain English (pseudocode). Don't write a single line of code yet!

I

Implement

Now translate your plan into code. Focus on the core logic first.

R

Review

Walk through your code with an example. Did you miss any edge cases?

E

Evaluate

What is the Time and Space complexity? Can you make it faster?

Congratulations!

You've completed the 80+ Coding Interview Series. You now have the foundation to tackle most entry-level technical interviews.