Implement Adjacent-Line Uniq
Company: Vanta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
# Implement Adjacent-Line Uniq
Implement the core behavior of the Unix-style `uniq` utility: combine consecutive equal lines into one run. The interview report names `uniq` but does not preserve its command-line flags, so the literal return shape and three modes below are explicit practice assumptions.
```python
def uniq_adjacent(
lines: list[str],
mode: str,
ignore_case: bool,
) -> list[list[object]]:
...
```
Return one `[line, count]` record for every selected run. `line` is the first original line in that run and `count` is the number of consecutive input lines with the same comparison key.
## Modes
- `mode == "all"`: return every run.
- `mode == "repeated"`: return only runs with `count > 1`.
- `mode == "unique"`: return only runs with `count == 1`.
When `ignore_case` is false, lines are equal only when their strings are exactly equal. When it is true, compare `line.casefold()` values but still preserve the first line's original spelling in output. Do not sort: equal lines separated by another line belong to different runs.
## Example
```text
Input:
lines = ["A", "a", "a", "B", "A"]
mode = "all"
ignore_case = true
Output:
[["A", 3], ["B", 1], ["A", 1]]
```
## Constraints and Errors
- `0 <= len(lines) <= 500_000`
- The total number of characters is at most `20_000_000`.
- `mode` must be exactly one of `"all"`, `"repeated"`, or `"unique"`.
- A non-list input, non-string line or mode, or non-Boolean `ignore_case` raises `ValueError`.
- Validate all line types before producing output.
- Do not mutate the input.
## Hints
- Keep the first original line, its comparison key, and the current run count.
- Emit a run when the key changes and once more after the loop.
- The algorithm should use linear time and constant auxiliary state apart from output.
Quick Answer: Implement adjacent-line uniq behavior without sorting, grouping only consecutive lines that share the selected exact or case-folded key. Support all, repeated, and unique run modes while preserving the first original spelling and reporting each run count in linear time.
Compress consecutive equal input lines into [first_original_line, count] runs, filtered by all, repeated, or unique mode with optional casefold comparison.
Constraints
- mode is all, repeated, or unique
- ignore_case is a Boolean
- Do not sort or mutate lines
Examples
Input: {'lines': [], 'mode': 'all', 'ignore_case': False}
Expected Output: []
Explanation: Empty input has no runs.
Input: {'lines': ['a'], 'mode': 'all', 'ignore_case': False}
Expected Output: [['a', 1]]
Explanation: A single line forms one run.
Hints
- Track the first original line, comparison key, and count for the current run.
- Emit when the key changes and once after the loop.