Katkat

Debug and test a Python function in venv

Capital OneData Scientist · Onsite

Last updated

This question evaluates understanding of Python runtime semantics (mutable default arguments, in-place list mutation during iteration), debugging and unit-testing practices, virtual environment and packaging workflows, and CI/tooling considerations.

Consider the Python snippet below.

""" import math

def accumulate(nums, start=0, cache={}): total = start for n in nums: if n % 2 == 0: nums.append(n//2) total += n cache[str(nums)] = total return total """

Tasks:

  1. Precisely state what accumulate([2, 3], start=1) returns and why (walk through iteration order and mutations). Identify every bug/code smell (logical, algorithmic, and design) and their impact: list-mutation during iteration, default-mutable arguments, potential non-termination, unintended cache growth, time/space complexity, and keying strategy.
  2. Provide a corrected, production-ready version (pure function + optional caching) with asymptotic complexity analysis and docstring that specifies pre/post-conditions.
  3. Write minimal but thorough pytest unit tests (property-based or table-driven) covering edge cases (empty list, very large even chains, negatives, None, non-ints) and proving termination.
  4. Show exact commands to: create/activate an isolated virtual environment; pin dependencies; run tests; and produce a wheel. Assume macOS/Linux.
  5. Briefly explain how you would add CI (static type checks with mypy, linting, coverage thresholds) and package an entry-point CLI.