Produce a Minimal Line-Level Diff Between Two Config File Versions
Last updated: November 29, 2026
Quick Overview
Given the old and new text of a configuration file, output a line-by-line diff where unchanged lines are prefixed with two spaces, removed lines with "- ", and added lines with "+ ", using the fewest possible added and removed lines.
0
0
0 solved
Given the old and new text of a configuration file, output a line-by-line diff where unchanged lines are prefixed with two spaces, removed lines with "- ", and added lines with "+ ", using the fewest possible added and removed lines.
Code review at Google (Critique) and config rollout tools show diffs all day, so implementing one is a natural onsite exercise. The interviewer checks that you reduce the problem to longest common subsequence, reconstruct an edit script correctly, and know the practical speedups and their limits.
What the Interviewer Expects
- Reduces minimal diff to longest common subsequence over lines
- Builds the LCS table and walks it to emit keep, add, and remove operations
- Trims common prefix and suffix before the quadratic step
- Verifies the diff by applying it to recover both versions
- Discusses Myers' O(ND) algorithm for large files with few changes
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Group the output into unified diff hunks with three lines of context.
- Files have a million lines and a handful of changes; how does Myers' algorithm or patience diff help?
- Produce word-level diffs inside a changed line.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: two strings; compare them as lists of lines.
- Output: a list of lines prefixed
" ","- ", or"+ "such that dropping+lines yields the old file and dropping-lines yields the ne...
High-Level Design
The kept lines form a common subsequence of both files, so a minimal diff keeps a longest common subsequence. Before the quadratic DP, strip the shared prefix and suffix; real edits are small, so this...