PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in shell scripting and Linux terminal automation for Python virtual-environment setup, focusing on environment management, automation, and reproducibility skills relevant to data science workflows.

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

Automate Python Virtual Environment Setup on Linux Terminal

Company: Capital One

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

##### Scenario Shell script that automates Python virtual-environment setup on a Linux terminal during a tech interview ##### Question Walk through the script line-by-line and explain exactly what each command does. What advantages does writing this logic as a shell script provide compared with other approaches? Run the script in the terminal and describe the expected side-effects or files that should appear. ##### Hints Think about shebang, `set -e`, `virtualenv`, `source`, permission bits, idempotency, and automation benefits such as reproducibility.

Quick Answer: This question evaluates proficiency in shell scripting and Linux terminal automation for Python virtual-environment setup, focusing on environment management, automation, and reproducibility skills relevant to data science workflows.

Simulate a simplified shell script that automates Python virtual-environment setup. Given a list of command strings, determine whether the script is idempotent: running it twice in sequence (the second run starts from the final state of the first) must succeed both times and leave the exact same final state. The simulation uses set -e semantics: on the first error, the run stops immediately. The file system starts with only the root directory. Paths are relative (no leading slash), use '/' as a separator, and contain no '.' or '..'. Supported commands and their effects: - set -e: no-op; errors always stop execution. - mkdir PATH: create PATH and any missing parent directories; error if any path segment conflicts with an existing file. - touch PATH: create an empty file; parent directory must exist; error if a directory exists at PATH. - rm PATH: remove a file or directory recursively; no-op if PATH does not exist. - chmod +x PATH: set executable bit on an existing file; error if PATH does not exist or is a directory. - virtualenv PATH: ensure PATH (directory), PATH/bin (directory), and PATH/bin/activate (file) exist; error if PATH exists as a file or if PATH/bin/activate exists as a directory. - source PATH: require PATH to exist as a file; if PATH ends with '/bin/activate', set environment variable ACTIVE_VENV to the virtualenv root (PATH without '/bin/activate'). Final state consists of the set of paths (with types file/dir), executable flags on files, and the value of ACTIVE_VENV. Return true if both runs succeed and the final states are identical; otherwise return false.

Constraints

  • 1 <= len(commands) <= 100000
  • Each command is one of: 'set -e', 'mkdir PATH', 'touch PATH', 'rm PATH', 'chmod +x PATH', 'virtualenv PATH', 'source PATH'
  • PATH is relative (no leading '/'), uses '/' as separator, has no '.' or '..', and contains no spaces
  • Each PATH length <= 200
  • Initial file system contains only the root directory

Hints

  1. Model the file system with hash maps: path -> type (file/dir) and executable bit for files.
  2. Implement mkdir with recursive creation; error if a file blocks any directory component.
  3. Implement rm as recursive deletion; maintain parent->children sets for efficient subtree removal.
  4. virtualenv PATH should ensure PATH, PATH/bin, and PATH/bin/activate exist; treat conflicts as errors.
  5. Apply set -e: stop the run on the first error. Idempotent means both runs succeed and final states match (including ACTIVE_VENV).
Last updated: Mar 29, 2026

Loading coding console...

PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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

  • Reorder a String by Alternating Its Left and Right Ends - Capital One (medium)
  • Arrange Match Results in Repeating Win-Draw-Loss Order - Capital One (medium)
  • Sort Every Concentric Matrix Border Clockwise - Capital One (medium)
  • Count Pairs of Cyclically Equivalent Integers - Capital One (medium)
  • Sort Matrix Diagonals By Their Values - Capital One (medium)