Explain Python Virtual Environment Setup and Function Analysis
Company: Capital One
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
##### Scenario
Technical screen where candidate and interviewer jointly walk through Python code snippets used in a data-science repository.
##### Question
Show how you would create, activate, and later remove a Python virtual environment on a Unix system. Given the following Python function (provided by interviewer), explain line-by-line what it does and its time/space complexity. Write unit tests that would fully cover edge cases for this function.
##### Hints
Focus on correct CLI commands, Python packaging best practices, docstrings, pytest usage, and explaining algorithmic behavior clearly.
Quick Answer: This question evaluates proficiency in Python environment management, code comprehension and line-by-line explanation, algorithmic time and space complexity analysis, and unit testing and packaging practices (including docstrings and pytest).
You are given a sequence of shell-like commands that manage Python virtual environments on a Unix system. Validate the sequence according to these rules:
- Allowed commands are exactly: "create NAME", "activate NAME", "deactivate", "remove NAME".
- Initially, no environments exist and no environment is active.
- create NAME: valid only if NAME does not already exist; then NAME is added to the set of environments.
- activate NAME: valid only if NAME exists and no environment is currently active; then NAME becomes the active environment.
- deactivate: valid only if an environment is currently active; then there is no active environment.
- remove NAME: valid only if NAME exists and NAME is not the active environment; then NAME is removed from the set of environments.
Return the 0-based index of the first invalid command. If all commands are valid, return -1.
Constraints
- 1 <= len(commands) <= 100000
- Each command is one of: 'create NAME', 'activate NAME', 'deactivate', 'remove NAME'
- NAME consists of lowercase letters, digits, underscores, or hyphens; 1 to 32 characters
- At most one environment can be active at any time
- Return the index of the first invalid command or -1 if all are valid
- Aim for O(n) time and O(n) space where n = len(commands)
Solution
from typing import List
def validate_venv_commands(commands: List[str]) -> int:
envs = set()
active = None
for i, raw in enumerate(commands):
s = raw.strip()
if not s:
return i # empty command invalid
parts = s.split()
op = parts[0]
if op == 'create':
if len(parts) != 2:
return i
name = parts[1]
if name in envs:
return i
envs.add(name)
elif op == 'activate':
if len(parts) != 2:
return i
name = parts[1]
if name not in envs or active is not None:
return i
active = name
elif op == 'deactivate':
if len(parts) != 1 or active is None:
return i
active = None
elif op == 'remove':
if len(parts) != 2:
return i
name = parts[1]
if name not in envs or active == name:
return i
envs.remove(name)
else:
return i # unknown command
return -1