cswork / libraryOpen practice workspace

Coding questions · Capital One

Explain Python Virtual Environment Setup and Function Analysis

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).

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.

Validate Virtual Environment Command Sequence

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)

Examples

Example 1

Input: commands = ["create env", "activate env", "deactivate", "remove env"]
Output: -1

Explanation: All commands follow the rules: create -> activate -> deactivate -> remove.

Example 2

Input: commands = ["create ds", "activate ds", "remove ds"]
Output: 2

Explanation: Cannot remove an active environment; must deactivate first.

Hints

  1. Track existing environments with a hash set and the active environment with a variable.
  2. Parse each command into tokens and validate arity (one or two tokens).
  3. Return immediately when a rule is violated to capture the first invalid index.

Approach

Simulate the environment manager. Maintain a hash set of existing environments and a variable for the active one. For each command, check validity in O(1) using the set and the active variable. Return the index at the first rule violation; otherwise, return -1 after processing all commands.

  • Time complexity: O(n)
  • Space complexity: O(n)