Git, Docker, Python Environments and Modern C++ Essentials
System DesignModel answerLast updated
Explain a team Git workflow, Docker image practices, Python isolation and pinning, and core C++ idioms (RAII, moves, const), with reasons and trade-offs.
You are in a broad technical discussion about everyday engineering practice. Answer the four parts below, giving the reasoning behind each rule of thumb and not just a list of commands or definitions.
- Git collaboration. Walk through how you would take a change from idea to merged code using a topic branch and a pull request. Say when you would rebase and when you would merge, and why.
- Docker. Explain the difference between an image, a container and a Dockerfile. Describe how you would make an image smaller and how you would keep credentials out of it.
- Python environments. Compare a virtual environment with a container as isolation mechanisms. Explain how you would pin dependencies for an application versus a reusable library.
- C++ idioms. Explain RAII, move semantics and const-correctness, with a short code example for each idea.
State any assumptions, call out common pitfalls, and say how you would check that your approach works in practice.
Hints
Hint 1
For Git, think about who else might hold copies of the commits you are about to rewrite. That decides rebase versus merge.
Hint 2
Docker layers are additive: deleting a file in a later layer does not shrink the earlier layer. Also ask where a secret could leak (layers, history, environment).
Hint 3
For pinning, separate what you intend to depend on (ranges) from what actually got installed (a full lock with hashes). Applications and libraries need different strategies.
Hint 4
In C++, ask what happens to a resource when an exception is thrown halfway through a function, and what a move leaves behind in the source object.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
What a strong answer shows
The four parts test whether you can work safely in a shared codebase (Git), produce reproducible artifacts (Docker, pinning), reason about isolation boundaries (venv vs. container) and write resource-safe native code (C++). The best answers give a rule and the reason behind it.
1. Git: topic branches, PRs, rebase vs. merge
Flow
git switch main && git pull --ff-only
git switch -c feature/retry-logic # short-lived topic branch
# small, focused commits
git push -u origin feature/retry-logic # open a PR; CI runs tests and lint
# address review feedback with more commits
git fetch origin && git rebase origin/main # sync a branch only you use
git push --force-with-lease # safe force-push after a rebase
# merge through the PR, then delete the branch
Rebase vs. merge
- Rebase replays your commits on a new base. It gives linear history, which makes
git bisectand reading logs easier. Use it on private branches, to pick up changes frommainor to tidy commits withrebase -i. - Merge keeps history as it happened and records the integration point. Use it for shared branches (
main, release branches) or when others have built on your commits. - Golden rule: do not rewrite commits that other people have already pulled. Their copies diverge and reconciling them is painful.
- Squash-merge gives one commit per PR, so reverts are trivial, at the cost of losing the intermediate commits.
- Prefer
--force-with-leaseto--force. It refuses to overwrite remote work you have not seen.
Pitfalls: long-lived branches that drift and cause painful conflicts, giant PRs that nobody reviews properly, and force-pushing to shared branches.
2. Docker
- Image: an immutable stack of read-only, content-addressed layers plus metadata (entrypoint, environment, exposed ports).
- Container: a running instance of an image. It adds a thin writable layer and runs inside isolated kernel namespaces with cgroup limits. Treat containers as disposable and keep state in volumes.
- Dockerfile: the recipe. Each instruction creates a cacheable layer.
Shrinking images
- Start from a small base (
-slim, distroless, or Alpine, remembering that musl can break some Python wheels). For GPU workloads choose theruntimeCUDA variant rather thandevel. - Use multi-stage builds: compile in a builder stage and copy only the outputs into the final stage.
- Clean up in the same
RUNthat created the mess (--no-install-recommends, remove apt lists,pip --no-cache-dir). A file deleted in a later layer is still stored in the earlier one. - Add a
.dockerignorefor.git, datasets and build output. - Order instructions for caching: copy dependency manifests and install first, then copy source, so code edits do not invalidate the dependency layer.
- Run as a non-root user.
Secrets
- Never put secrets in
COPY,ENVorARG. They remain in layers and show up indocker history. - For build-time needs (a private package index token), use BuildKit secret mounts, which are not written to any layer.
- At runtime, inject secrets from an orchestrator or secret manager (Kubernetes Secrets, Vault, cloud services), ideally as mounted files rather than environment variables.
# syntax=docker/dockerfile:1
FROM python:3.12-slim AS build
WORKDIR /app
COPY requirements.lock .
RUN --mount=type=secret,id=idx_token \
PIP_INDEX_URL="https://__token__:$(cat /run/secrets/idx_token)@pypi.example.internal/simple" \
pip install --no-cache-dir --prefix=/deps -r requirements.lock
FROM python:3.12-slim
COPY --from=build /deps /usr/local
COPY src/ /app/src
USER 10001
ENTRYPOINT ["python", "-m", "src.main"]
Build with docker build --secret id=idx_token,src=token.txt .. To validate, run docker history and inspect the layers, and scan the image for vulnerabilities and leaked credentials.
3. Python: venv vs. container, and pinning
| Virtual environment | Container | |
|---|---|---|
| Isolates | Python packages | Filesystem, OS libraries, interpreter, processes, network |
| Shares with host | Interpreter, libc, system libraries, drivers | Kernel (and GPU driver via the NVIDIA toolkit) |
| Cost | Nearly free, seconds to create | Build and storage cost, needs a runtime |
| Best for | Local development, tooling | CI/production parity, deployment |
They complement each other. A venv gives a fast inner loop; a container guarantees identical OS packages and native libraries everywhere. A venv inside a container is fine and keeps app packages apart from the system Python.
Pinning
- Applications: declare direct dependencies with compatible ranges in
pyproject.toml, then produce a lock file with exact versions and hashes for every transitive dependency (uv lock,pip-compile --generate-hashes, Poetry). Install only from the lock in CI and in the image. - Libraries: publish ranges, not exact pins, so consumers can resolve conflicts between packages.
- Why: it prevents transitive drift ("works on my machine"), makes builds reproducible and, with hash checking, protects against tampered packages.
- Avoid rot: use Dependabot or Renovate so updates arrive as reviewed PRs that CI tests.
4. C++: RAII, moves, const-correctness
RAII. Tie a resource's lifetime to an object's. The constructor acquires, the destructor releases, and destructors run on every exit path, including exceptions. This removes leaks and forgotten unlocks. Standard examples are std::unique_ptr, std::lock_guard and std::fstream.
Move semantics. An rvalue reference (T&&) binds to temporaries and to objects passed through std::move. A move constructor takes over the source's resource in O(1) rather than deep copying in O(n), leaving the source valid but unspecified. Mark moves noexcept, or std::vector will copy on reallocation to keep its strong exception guarantee. Prefer the rule of zero by composing RAII members; if you own a raw resource, apply the rule of five.
Const-correctness. Use const to state and enforce what does not change: pass large read-only objects as const T&, mark non-mutating member functions const, and distinguish const T* (pointee is const) from T* const (pointer is const).
#include <cstdio>
#include <stdexcept>
#include <string>
#include <utility>
class FileHandle { // RAII wrapper
public:
explicit FileHandle(const std::string& path) // const ref: no copy, no mutation
: fp_(std::fopen(path.c_str(), "r")) {
if (!fp_) throw std::runtime_error("cannot open file");
}
~FileHandle() { if (fp_) std::fclose(fp_); } // released on every exit path
FileHandle(const FileHandle&) = delete; // single owner
FileHandle& operator=(const FileHandle&) = delete;
FileHandle(FileHandle&& o) noexcept : fp_(std::exchange(o.fp_, nullptr)) {}
FileHandle& operator=(FileHandle&& o) noexcept {
if (this != &o) {
if (fp_) std::fclose(fp_);
fp_ = std::exchange(o.fp_, nullptr);
}
return *this;
}
bool is_open() const { return fp_ != nullptr; } // const member function
private:
std::FILE* fp_;
};
Moving transfers the FILE* and nulls the source, so exactly one object closes it. Cost is O(1) per move.
Pitfalls: forgetting noexcept on moves, using an object after std::move as if it still held its value, and returning references to locals.
Closing checks
Show you can verify your claims: CI plus a clean-clone build for Git and lock files, docker history and image scanning for Docker, a from-scratch install from the lock file for Python, and sanitizers (ASan/UBSan) plus tests with injected exceptions for C++.