Interviewers often use short Python scripts to test whether you can trace execution, identify recursion, and explain behavior clearly.
You are given a short Python script and asked to explain exactly what it does. Your answer should cover:
For example, consider a script that repeatedly divides a number by 10, processes the remaining prefix recursively, and combines results using modulo arithmetic. Explain the order of execution, how digits are handled, and what value is returned.
The interviewer is not looking for a rewritten implementation first. They want a precise walkthrough of control flow, intermediate reasoning, and the final behavior of the script. You should also mention the time and space complexity and point out any assumptions, such as the input being a non-negative integer.
A recursive function must stop at some condition; otherwise it calls itself forever. In digit-processing scripts, the base case is often when the number becomes 0 or has only one digit left.
if n == 0:
return 0
These scripts usually reduce the problem size by removing one digit at a time, commonly with n // 10. That lets the function solve a smaller version of the same problem before combining the current digit.
return f(n // 10) + (n % 10)
Modulo and integer division are standard tools for working with decimal digits. n % 10 gets the last digit, while n // 10 removes the last digit.
last = n % 10
rest = n // 10
Recursive calls go deeper before results are combined on the way back up. That means understanding the script requires tracking both the descent into smaller inputs and the unwinding of returned values.