Document Version Control System
Company: Workday
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a document version control system that tracks the complete edit history of documents.
Every document starts at version 1 when created. Each call to edit creates a new version (incrementing by 1) while preserving all prior versions. Users can fetch either the latest version or any specific historical version of a document.
If a requested document or version does not exist, return the string `"NOT_FOUND"`.
Implement the following methods:
- `create(doc_id: int, content: str)`: Create a new document with the given content as version 1. If the document already exists, do nothing.
- `edit(doc_id: int, content: str) -> int`: Append a new version with the given content and return the new version number. Returns -1 if the document does not exist.
- `get_latest(doc_id: int) -> str`: Return the content of the latest version of the document, or `"NOT_FOUND"` if the document does not exist.
- `get_version(doc_id: int, version: int) -> str`: Return the content at the specified version, or `"NOT_FOUND"` if the document or version does not exist.
- `get_latest_version_number(doc_id: int) -> int`: Return the latest version number of the document, or -1 if the document does not exist.
Example 1:
```text
Input:
["DocumentVersionControl", "create", "edit", "edit", "get_latest", "get_version", "get_version", "get_latest_version_number"]
[[], [1, "Hello"], [1, "Hello World"], [1, "Hello World!"], [1], [1, 1], [1, 2], [1]]
Output: [null, null, 2, 3, "Hello World!", "Hello", "Hello World", 3]
Explanation:
vcs = DocumentVersionControl() # empty system
vcs.create(1, "Hello") # doc 1 = [v1: "Hello"]
vcs.edit(1, "Hello World") # -> 2 (doc 1 now has v2)
vcs.edit(1, "Hello World!") # -> 3 (doc 1 now has v3)
vcs.get_latest(1) # -> "Hello World!"
vcs.get_version(1, 1) # -> "Hello"
vcs.get_version(1, 2) # -> "Hello World"
vcs.get_latest_version_number(1) # -> 3
```
Example 2:
```text
Input:
["DocumentVersionControl", "get_latest", "edit", "create", "create", "get_latest", "get_version", "get_latest_version_number"]
[[], [42], [42, "ignored"], [42, "first"], [42, "duplicate"], [42], [42, 5], [99]]
Output: [null, "NOT_FOUND", -1, null, null, "first", "NOT_FOUND", -1]
Explanation:
vcs = DocumentVersionControl() # empty system
vcs.get_latest(42) # -> "NOT_FOUND" (doc 42 missing)
vcs.edit(42, "ignored") # -> -1 (cannot edit missing doc)
vcs.create(42, "first") # doc 42 = [v1: "first"]
vcs.create(42, "duplicate") # ignored, doc 42 already exists
vcs.get_latest(42) # -> "first"
vcs.get_version(42, 5) # -> "NOT_FOUND" (v5 does not exist)
vcs.get_latest_version_number(99) # -> -1 (doc 99 missing)
```
```hint Version numbers as positions
Look for a layout where a version number maps straight to a position in storage, then check which version numbers fall outside that range.
```
### Constraints & Assumptions
- `1 <= doc_id <= 10^5`
- `1 <= version <= 10^4`
- `1 <= len(content) <= 200`
- At most 10,000 total operations across `create`, `edit`, `get_latest`, `get_version` and `get_latest_version_number`.
### Clarifying Questions to Ask
- Should `create` on an existing document overwrite it, reset its history, or do nothing?
- Does an edit whose content matches the latest version still count as a new version?
- Do documents or versions ever need to be deleted, or does the history only grow?
- Is memory a concern for long histories, so that versions might be stored as diffs instead of full copies?
### What a Strong Answer Covers
- A data layout that maps a document and a version number to its content in constant time.
- Version numbering that starts at 1 and grows by exactly 1 on every edit.
- Correct results for missing documents, duplicate creates, edits to missing documents, and out-of-range versions (0, negative, past the latest).
- The time cost of each operation and the total memory used.
### Follow-up Questions
- How would you store versions as diffs to save space, and what would that do to the cost of reading an old version?
- How would you let a user revert a document to an earlier version?
- How would you handle two users editing the same document at the same time?
- How would you support deleting a document or pruning old versions?
Overview: Design a small version control system that creates documents, records every edit as a new numbered version and returns the latest or any earlier version on request. It tests choosing a data layout with constant-time reads and handling missing documents and out-of-range version numbers.