Design an in-memory version control system with commits, branches, checkout and rollback
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a simplified in-memory version control system that mimics core Git-like behavior. The system manages a staging area, commits (immutable snapshots of files) and branches (named pointers to commits).
Each commit captures the full set of tracked files at the time of the commit and is identified by a deterministic commit ID of the form `c1`, `c2`, `c3`, ..., assigned in commit order. Branches track linear commit histories, and `HEAD` follows the currently checked out branch or commit.
The initial branch is named `main`. Switching branches or checking out a commit restores the corresponding file contents into the working area. Rollback moves the current branch's `HEAD` backward to a prior commit reachable from its history.
Implement a `VersionControlSystem` class with these methods:
- `add(file: str, content: str) -> None` stages the file with the given content, overwriting any content staged earlier for that file.
- `commit(message: str) -> str` snapshots the working area, including all staged changes, as a new commit on the current branch and returns its commit ID.
- `branch(name: str) -> None` creates a new branch with the given name that points at the current HEAD commit.
- `checkout(target: str) -> None` switches the working area to the given branch name or commit ID and restores its file snapshot.
- `rollback(commit_id: str) -> None` moves the checked-out branch's HEAD back to the given commit, which is reachable from the branch's history, and restores that commit's files.
- `get_file(file: str) -> str` returns the current working content of the file, or an empty string if the file does not exist in the working area.
- `current_branch() -> str` returns the name of the checked-out branch, or the commit ID if HEAD is detached.
Example 1:
```text
Input:
["VersionControlSystem", "add", "commit", "branch", "checkout", "add", "commit", "get_file", "checkout", "get_file", "current_branch"]
[[], ["a.txt", "v1"], ["first"], ["dev"], ["dev"], ["a.txt", "v2"], ["edit on dev"], ["a.txt"], ["main"], ["a.txt"], []]
Output:
[null, null, "c1", null, null, null, "c2", "v2", null, "v1", "main"]
Trace:
vcs = VersionControlSystem()
vcs.add("a.txt", "v1")
vcs.commit("first") # "c1", main -> c1
vcs.branch("dev") # dev -> c1
vcs.checkout("dev")
vcs.add("a.txt", "v2")
vcs.commit("edit on dev") # "c2", dev -> c2, main stays at c1
vcs.get_file("a.txt") # "v2"
vcs.checkout("main") # restores the snapshot of c1
vcs.get_file("a.txt") # "v1"
vcs.current_branch() # "main"
```
Creating `dev` points it at `c1`. The commit made on `dev` moves only `dev`, so checking out `main` brings back the snapshot of `c1`.
Example 2:
```text
Input:
["VersionControlSystem", "add", "commit", "add", "commit", "add", "commit", "checkout", "current_branch", "get_file", "checkout", "rollback", "get_file", "get_file", "commit"]
[[], ["a.txt", "1"], ["m1"], ["b.txt", "x"], ["m2"], ["a.txt", "3"], ["m3"], ["c1"], [], ["b.txt"], ["main"], ["c2"], ["a.txt"], ["b.txt"], ["m4"]]
Output:
[null, null, "c1", null, "c2", null, "c3", null, "c1", "", null, null, "1", "x", "c4"]
Trace:
vcs = VersionControlSystem()
vcs.add("a.txt", "1")
vcs.commit("m1") # "c1", snapshot {a.txt: "1"}
vcs.add("b.txt", "x")
vcs.commit("m2") # "c2", snapshot {a.txt: "1", b.txt: "x"}
vcs.add("a.txt", "3")
vcs.commit("m3") # "c3", snapshot {a.txt: "3", b.txt: "x"}
vcs.checkout("c1") # HEAD detached at c1
vcs.current_branch() # "c1"
vcs.get_file("b.txt") # "" because b.txt did not exist at c1
vcs.checkout("main") # back on main, at c3
vcs.rollback("c2") # main -> c2
vcs.get_file("a.txt") # "1"
vcs.get_file("b.txt") # "x"
vcs.commit("m4") # "c4", parent c2, main -> c4
```
Checking out `c1` detaches HEAD, so `current_branch` returns the commit ID, and `b.txt` is not in that snapshot. The rollback walks back from `c3` to `c2` and restores both files as they were at `c2`. Commit IDs come from one counter for the whole system, so the next commit is `c4`, and its parent is `c2`.
```hint What HEAD can point at
HEAD is either a branch name or a bare commit ID. Decide how you store that so `commit`, `branch` and `current_branch` can each tell the two cases apart.
```
```hint Snapshots must not share state
After a commit, later `add`, `commit` and `checkout` calls must never change what that commit holds. Check every place where your code copies a dict and every place where it only passes a reference.
```
### Constraints & Assumptions
- At most `10^4` operations across all methods.
- File names, contents, branch names and commit messages are non-empty strings of length at most 100.
- The initial branch is `main` and has no commits until the first `commit` call.
- Commit IDs are assigned as `c1`, `c2`, `c3`, ... in commit order.
- `checkout` targets are either an existing branch name or an existing commit ID.
- `rollback` is only called when `HEAD` is attached to a branch, and the target commit ID is reachable from that branch's current HEAD.
### Clarifying Questions to Ask
- Does `get_file` see content that was staged with `add` but not committed yet?
- What happens to staged changes on `checkout` or `rollback`: are they discarded or carried over?
- If `commit` is called while HEAD is detached, does HEAD move to the new commit, and does any branch move?
- What should `branch` do when the name already exists, and is `branch` allowed on `main` before its first commit?
- Can commits that a rollback leaves behind still be checked out by their ID?
### What a Strong Answer Covers
- A state model for the staging area, the working area, the commit store, the branch table and HEAD, including how attached and detached HEAD differ.
- How a commit's snapshot stays fixed while the working area and staging area keep changing.
- Correct behavior in the edge states: `main` before its first commit, a detached HEAD, and a rollback that leaves later commits off the branch's history.
- The semantics the prompt leaves open, each stated and resolved explicitly.
- The time and space cost of `commit`, `checkout` and `rollback`, and how storage grows with full snapshots.
### Follow-up Questions
- Full snapshots copy every file on every commit. How would you store commits so unchanged files are shared between them?
- How would you add `merge(branch)`, including finding the common ancestor and reporting conflicts?
- How would you add `log()` for the current branch's history and `diff(a, b)` between two commits?
- What changes if commit IDs become content hashes instead of a counter?
Overview: Design an in-memory version control system with a staging area, immutable commit snapshots, branches, checkout by branch or commit ID, and rollback along a branch's history. It tests state modeling for attached and detached HEAD, copying snapshots safely, and stating unclear semantics before writing code.