statslink

linking statistics to all

Some Python interview questions (Part IV)

I recently encountered another technical interview which posed the question in an online coding debug format. The interviewer gave me a URL from SharePad.io and asked the question: “Do you see anything wrong with the code?”

#!/usr/bin/python2

# This code sums the squares of all numbers between start_number and end_number,
# Added to any numbers in the optional argument initial_series,
# Then writes that number out to a file.

# e.g. sum(2, 4, [7, 9]) = 4 + 9 + 7 + 9 = 29

def sum(start_number, end_number, initial_series=[]):
    output = file('output.txt', 'w')

    for number in range(start_number, end_number):
        square = number*number
        initial_series.append(square)

    sum = 0
    for number in initial_series:
        sum = sum + number

    output.write("%s" % sum)


# Now test
sum(2, 4, [7, 9])

The interviewer gave me about 5 to 10 minutes while he watched. The process was intimidating and there was no time to run the code which is what typically happens in a real-world setting. Undoubtably, I failed this portion of the interview but now that I’ve had time to dig deeper into the code. First, the code was written in Python 2, and as of the time of this post we are on Python 3.12.3 so it was difficult to assess whether any unfamiliar syntax was due to the Python version or a genuine syntax error. Second, the interviewer was not specific about whether there was a logic error in the code and I spent all my time focusing on the logic of the code.

Let’s try running this code in the most recent version of Python. The first error was NameError: name 'file' is not defined which (as I thought) is due to an obsolete function from Python 2, which as been replaced with open(). Since the point isn’t to write the output to a file, let’s just comment that line out and the line output.write("%s" % sum) which invokes the write method and replace it with return(sum) which simply returns the object sum. And guess what the result is when I ran it in Google Colab? 29. So as it turns out there is nothing wrong with the logic of the code.

Given that there is nothing wrong with the code, then what is the interviewer trying to get at? I feel it was a bit misleading because I asked him if there’s anything wrong with the code and he said yes which I assumed meant that there was something wrong with the code, but apparently not.

The only thing that struck out to me was the use of the function name sum() which I know is a built-in Python function. Opening another session, I typed ?sum which returned the following:

Signature: sum(iterable, /, start=0)
Docstring:
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
Type:      builtin_function_or_method

The sum() function is a built-in function in Python that takes an iterable. An iterable is a Python object that can be looped over such as in a for loop. The list (e.g., [1,2,3]) is the most common iterable, but tuples, dictionaries, and strings are all iterable. This means that if I enter the command sum([1,2,3]) it returns 6. The / in the second parameter means that the iterable is positional and cannot be a keyword. For example, sum(iterable=[1,2,3]) returns the error TypeError: sum() takes at least 1 positional argument (0 given). Finally, the start=0 adds a number to the sum. For example, sum([1,2,3],10) returns 16.

Back to the interview question, since we used the name sum() to define the function it becomes scoped as a top-level function which overrides the built-in function with the same name. As a result when I enter the command sum([1,2,3]) it returns TypeError: sum() missing 1 required positional argument: 'end_number' since we defined the function as taking in the following three parameters: start_number, end_number, initial_series=[] which corresponds to two integers and a list, it is expecting this three inputs, where we only provided the first (an moreover the function expects an integer and not a list of integers). A better alternative would be to use another name such as sum_squares() which is not a built-in Python function. A quick check is to use ?sum_squares to check if there is a help file associated with the function which returns Object `sum_squares` not found. This is the first interview question I’ve encountered that does not try to debug a logic flaw with the code but is focused on the naming conventions of the function itself.