WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Coding Interview Essentials · Time and Space Complexity Analysis · question 27 of 120

How does the time complexity of an algorithm affect the performance?

📕 Buy this interview preparation book: 120 Coding Interview Essentials questions & answers — PDF + EPUB for $5

The time complexity of an algorithm quantifies the amount of time an algorithm takes to run as a function of the size of the input to the program. It is usually estimated by counting the number of elementary operations performed by the algorithm, assuming that each elementary operation takes a fixed amount of time to perform. Therefore, the time complexity effectively measures how the run time grows — more or less — as the size of input increases.

To illustrate this, let’s consider a simple function that sums up all numbers in a list. Here’s the function in Python:

def sum_numbers(lst):
    result = 0
    for x in lst:
        result += x
    return result

In the above function, the time complexity is O(n) (often referred to as linear time complexity) where ‘n‘ is the length of the list ‘lst‘. This is because each additional element in the list will take a constant amount of time to process.

Conversely, if you have a function that must compare every element to every other element (say, to find duplicates), you are looking at a time complexity of O(n2) (often referred to as quadratic time complexity). Here’s an example function:

def has_duplicates(lst):
    n = len(lst)
    for i in range(n):
        for j in range(i+1, n):
            if lst[i] == lst[j]:
                return True
    return False

In the above function, each addition to the list ‘lst‘ can potentially create a new comparison to be done with all the other elements in the list.

In terms of performance, an algorithm that has lower time complexity will generally be faster than an algorithm with higher time complexity, especially as the input size becomes larger.

Here’s a simple chart showing the growth rates for different time complexities:

Time complexity    Growth rate
O(1)               Constant
O(log n)           Logarithmic
O(n)               Linear
O(n log n)         Log linear
O(n^2)             Quadratic
O(n^3)             Cubic
O(2^n)             Exponential

You can see that as we move down the table, the growth rate increases substantially. With large inputs, a time complexity difference can mean the difference between a result in a sensible time or waiting too long to ever see the result.

Please note that time complexity doesn’t tell the whole story; there are other factors at play, such as the space complexity (the amount of memory an algorithm uses), the programming language and environment, the constant factors in the time complexity (which are ignored in Big O notation but can matter in practice), and so on. But it’s a good starting point for analyzing the performance of an algorithm.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Coding Interview Essentials interview — then scores it.
📞 Practice Coding Interview Essentials — free 15 min
📕 Buy this interview preparation book: 120 Coding Interview Essentials questions & answers — PDF + EPUB for $5

All 120 Coding Interview Essentials questions · All topics