Debug and test a Python function in venv
Company: Capital One
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: 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.
Part 1: Simulate the buggy accumulate function and report the design issues
Constraints
- 0 <= len(nums) <= 10^5
- -10^18 <= nums[i], start <= 10^18
- nums contains only integers
- If 0 appears in nums, the original function would not terminate
Examples
Input: ([2, 3], 1)
Expected Output: (True, 7, [2, 3, 1], ['default_mutable_cache', 'input_list_mutation_during_iteration', 'keying_by_stringified_mutable_list', 'potential_nontermination_when_zero_present', 'quadratic_time_from_repeated_stringify', 'unbounded_cache_growth_across_calls'])
Explanation: 2 is visited first, so 1 is appended. The loop then visits 3 and finally the appended 1. Total = 1 + 2 + 3 + 1 = 7.
Input: ([], 5)
Expected Output: (True, 5, [], ['default_mutable_cache', 'input_list_mutation_during_iteration', 'keying_by_stringified_mutable_list', 'potential_nontermination_when_zero_present', 'quadratic_time_from_repeated_stringify', 'unbounded_cache_growth_across_calls'])
Explanation: The loop body never runs, so the function returns start unchanged.
Hints
- In Python, a for-loop over a list can visit items appended before the loop ends.
- Among integers, repeatedly applying n // 2 to an even number only loops forever for 0.
Part 2: Implement a pure, terminating accumulate with optional caching
Constraints
- 0 <= len(nums) <= 10^5 for valid cases
- -10^18 <= nums[i], start <= 10^18 for valid cases
- bool values are considered invalid even though bool is a subclass of int in Python
Examples
Input: ([2, 3], 1, False)
Expected Output: 7
Explanation: 2 contributes 2 + 1 = 3, 3 contributes 3, and start is 1.
Input: ([4, 4], 0, True)
Expected Output: 14
Explanation: Each 4 contributes 7, so the result is 14. Caching avoids recomputing the second chain.
Hints
- Process each number independently; you do not need to append into the original list.
- A repeated number should have the same contribution every time, which makes memoization natural.
Part 3: Choose a minimal table-driven pytest suite
Constraints
- 1 <= number of candidates <= 20
- 0 <= number of required tags <= 15
- Candidate names are unique strings
Examples
Input: ([('test_empty', ['empty', 'termination']), ('test_chain', ['chain', 'termination']), ('test_neg', ['negative']), ('test_invalid', ['none', 'nonint']), ('test_mixed', ['negative', 'chain'])], ['empty', 'chain', 'negative', 'none', 'nonint', 'termination'])
Expected Output: ['test_empty', 'test_invalid', 'test_mixed']
Explanation: This is the unique minimum cover of size 3.
Input: ([('aa_empty_term', ['empty', 'termination']), ('ab_invalid_chain', ['none', 'nonint', 'chain']), ('ac_negative', ['negative']), ('ba_empty_chain', ['empty', 'chain']), ('bb_invalid_term', ['none', 'nonint', 'termination']), ('bc_negative', ['negative'])], ['empty', 'chain', 'negative', 'none', 'nonint', 'termination'])
Expected Output: ['aa_empty_term', 'ab_invalid_chain', 'ac_negative']
Explanation: There are two minimum covers of size 3; this one is lexicographically smaller.
Hints
- Map each required tag to a bit position, then each test becomes a bitmask.
- Dynamic programming over covered-tag masks can find the minimum-size subset with deterministic tie-breaking.
Part 4: Generate exact venv, dependency, test, and wheel build commands
Constraints
- 0 <= total dependency entries <= 200
- Versions use numeric dot-separated semantic versioning such as 1.2.3
- Package names are non-empty strings without spaces
Examples
Input: ("app", [('requests', '2.32.3')], [('pytest', '8.1.1')])
Expected Output: ['cd app', 'python3 -m venv .venv', 'source .venv/bin/activate', 'python -m pip install --upgrade pip', 'python -m pip install build==1.2.1 pytest==8.2.2 requests==2.32.3', 'python -m pip freeze > requirements-lock.txt', 'pytest -q', 'python -m build --wheel']
Explanation: pytest is bumped to the required pinned version, and build is added.
Input: ("proj", [('Requests', '2.31.0'), ('requests', '2.32.3')], [('build', '1.0.0'), ('flake8', '7.1.0')])
Expected Output: ['cd proj', 'python3 -m venv .venv', 'source .venv/bin/activate', 'python -m pip install --upgrade pip', 'python -m pip install build==1.2.1 flake8==7.1.0 pytest==8.2.2 requests==2.32.3', 'python -m pip freeze > requirements-lock.txt', 'pytest -q', 'python -m build --wheel']
Explanation: requests is deduplicated case-insensitively and the higher version is kept.
Hints
- Normalize package names to lowercase before deduplicating.
- Compare semantic versions component by component, not as raw strings.
Part 5: Build a deterministic CI plan and CLI entry-point mapping
Constraints
- 0 <= len(features) <= 4
- features contains strings
- coverage_threshold is only validated when 'coverage' is enabled
Examples
Input: (['lint', 'mypy', 'coverage', 'cli'], 90, 'accumulate', 'pkg.cli:main')
Expected Output: (['python -m pip install -e .[dev]', 'ruff check .', 'mypy src', 'pytest --cov=src --cov-fail-under=90', 'python -m build'], 'accumulate=pkg.cli:main')
Explanation: setup runs first, then lint/mypy/test in alphabetical order, and build runs last.
Input: (['cli'], 80, 'tool', 'app.main:run')
Expected Output: (['python -m pip install -e .[dev]', 'python -m build'], 'tool=app.main:run')
Explanation: Only setup and build are needed.
Hints
- Treat each CI action as a graph node and use topological sorting.
- A deterministic tie-break rule is needed whenever several jobs become available together.