PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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

  • Medium
  • Capital One
  • Coding & Algorithms
  • Data Scientist

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)

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.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve Four Coding Assessment Tasks - Capital One (medium)
  • Write SQL using joins and window functions - Capital One (medium)
  • Review Preprocessing Code and Tests - Capital One (easy)
  • Solve multiple algorithmic interview questions - Capital One (hard)
  • Remove nodes with a given value - Capital One (medium)