0
0
Data Analysis Pythondata~5 mins

Customer segmentation pattern in Data Analysis Python - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Customer segmentation pattern
O(n)
Understanding Time Complexity

We want to understand how the time needed to group customers grows as the number of customers increases.

How does the work change when we have more customers to segment?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


import pandas as pd

def segment_customers(df):
    segments = {}
    for _, row in df.iterrows():
        key = (row['age_group'], row['income_level'])
        segments.setdefault(key, []).append(row['customer_id'])
    return segments
    

This code groups customers by their age group and income level into segments.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping through each customer record once.
  • How many times: Exactly once per customer, so as many times as there are customers.
How Execution Grows With Input

As the number of customers grows, the time to process grows in a straight line.

Input Size (n)Approx. Operations
1010 operations
100100 operations
10001000 operations

Pattern observation: Doubling customers roughly doubles the work.

Final Time Complexity

Time Complexity: O(n)

This means the time grows directly with the number of customers.

Common Mistake

[X] Wrong: "Grouping customers by multiple features means the time grows much faster, like squared."

[OK] Correct: The code only loops once through the data, grouping happens during that single pass, so time grows linearly, not squared.

Interview Connect

Understanding how grouping scales helps you explain data processing steps clearly and confidently in real projects.

Self-Check

"What if we nested loops to compare each customer with every other customer? How would the time complexity change?"