Interview Prep: Python for DevOps
Final assessment: comprehensive Q&A covering all 15 lessons. Test your knowledge, identify gaps, and prepare for technical interviews.
🎯 Beginner-Level Questions
Core fundamentals; expected on every interview.
Python offers better readability, maintainable code, rich libraries (requests, Kubernetes SDK), cross-platform compatibility, and strong error handling. Bash excels at command chaining but fails at complex logic; Python scales as infrastructure automation grows.
Mutable types (list, dict, set) can be changed after creation. Immutable types (int, str, tuple, bool) cannot. Example: `lst = [1,2]; lst[0] = 99` works. `s = "hi"; s[0] = 'x'` fails. Immutable types are safer for use as dict keys and in multi-threaded code.
The with statement guarantees cleanup code runs even if an exception occurs. For files: `with open(...) as f:` ensures f.close() happens automatically. This prevents file descriptor leaks. Context managers are essential for resource management—always use with for files, database connections, locks.
```python import time def retry_network_call(func, retries=3, wait=2): for i in range(retries): try: return func() except Exception as e: if i == retries - 1: return None print(f"Attempt {i+1} failed, retrying...") time.sleep(wait) ```
Lambda is an anonymous function: `lambda x: x * 2`. Use for short, throwaway operations passed to map/filter/sort. Example: sort servers by port: `sorted(servers, key=lambda s: s["port"])`. Avoid complex logic in lambda—use def for readability instead.
🎯 Intermediate-Level Questions
Applied knowledge; expected on senior roles.
Write to a temporary file, then atomically rename it to the target. Temp files ensure incomplete writes don't corrupt the config. Pattern: 1. Read original, 2. Write to temp file, 3. Rename temp to original. If crash during 1-2, original remains untouched; rename is atomic.
Use defaultdict(defaultdict(int)): outer key is service, inner key is error_type, value is count. For parsing variety: check if JSON first (json.loads), fall back to regex patterns. Regex should capture service name and error category. Output aggregated results as dict or JSON for downstream processing.
Python exceptions inherit from BaseException. Specific catching (FileNotFoundError, TimeoutError) lets you handle each error appropriately—retry timeout, skip missing file, raise on unexpected errors. Bare except() hides bugs and is dangerous—never use it. Good pattern: specific exceptions first, generic Exception as fallback, never catch BaseException.
Nest API calls with try/except at each level. Log before each call + result. Example: 1. Get users (log "Fetching users..."), 2. For each user, get details, 3. For each detail, get metrics. Catch specific errors (HTTP 404=skip, 500=retry, parse error=log&skip). Chain dependencies explicitly—don't proceed if upstream fails.
Use SDKs when available (azure-mgmt-compute, kubernetes client)—they're type-safe, handle auth/retries. Use subprocess for tools not in SDKs (kubectl, docker, terraform) or when SDK is overkill. SDKs are preferable for production; subprocess is pragmatic for CLIs and older systems.
🎯 Scenario-Based Questions
Real-world decision-making; tests judgment and trade-offs.
Use concurrent requests (threading/asyncio). Python's requests library + ThreadPoolExecutor: submit 20 requests in parallel, wait for results. Or use batch APIs (list all pods in one call + parse, not one call per pod). Add caching (in-memory dict with TTL) to avoid redundant checks within seconds. Profile to find bottleneck—is it network latency, parsing, or logic?
1. Immediately rotate credentials (they're compromised). 2. Read from environment variables: `os.getenv("API_KEY")`. 3. Use secure vaults (Azure Key Vault, AWS Secrets Manager) for production. 4. Add pre-commit hooks to catch secrets in code. 5. Code review policy: no secrets ever. Review all existing code for leaked credentials.
CI/CD environment has different PATH. Use absolute path: `/usr/bin/kubectl` instead of `kubectl`. Or install/setup in CI beforehand. Check environment in CI logs. Use which kubectl locally to find path, add to script. For subprocess: verify command exists before calling: `shutil.which("kubectl")` returns None if not found—handle gracefully.
Config-driven design: store settings in files (YAML/JSON) per environment or a single file with env flags. Load config, validate, apply. Example: replicas = config["environments"][env]["replicas"]. Use argparse: `deploy.py deploy --env prod --replicas 5 --override`. Code is unchanged; config varies. Alternately: feature flags in code (if env == "prod": replicas=5).
Set log level to WARNING (skip INFO/DEBUG). For deep debugging, temporarily raise level to DEBUG. Use structured (JSON) logging and send to log aggregation (ELK, Datadog)—they index and filter server-side, storage is cheap there. Locally, rotate logs (logrotate) and compress old files. Only log important state changes, not every operation.
📊 Self-Assessment Checklist
Rate your confidence (1-5) on each topic:
- ☐ Python basics (variables, types, operators)
- ☐ Control flow (if/elif/else, loops, break/continue)
- ☐ Functions, modules, and code organization
- ☐ File I/O and directory operations
- ☐ Regular expressions (pattern matching)
- ☐ JSON/YAML parsing and generation
- ☐ HTTP requests and API integration
- ☐ subprocess command execution
- ☐ Cloud SDKs (Azure, Kubernetes)
- ☐ Logging and error handling
- ☐ Building CLI tools with argparse
- ☐ Practical integration (combining multiple skills)
Weak areas (< 3): review corresponding lesson. Strong areas (5): mentor others!
💡 Interview Tips
- Show your work: explain as you code, not after.
- Start simple: get a basic solution working first, then optimize.
- Handle errors: show try/except/finally, don't ignore edge cases.
- Ask clarifying questions: assume nothing about requirements.
- Use Python idioms: list comprehensions, context managers, enumerate()—show you know the language.
- Discuss trade-offs: "subprocess is quick to implement but hard to debug; SDK is robust but requires learning." Interviewers value judgment.
- Test your code: mentally run through with sample input, spot bugs.