Keep the First Occurrence of Every Globally Unique Line
Company: Vanta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Remove repeated lines globally while preserving first-occurrence order, exact case, spaces, and empty lines.
Read the full Vanta Software Engineer interview experience this question came from
Constraints
- 0 <= len(lines) <= 200000.
- Each line contains printable ASCII characters with code points 32 through 126, or is empty.
- Total input character count is at most 2000000.
- Do not mutate the input.
- Input strings represent lines without their terminating newline characters.
Examples
Input: ([],)
Expected Output: []
Explanation: Minimum valid input: no lines, so nothing can occur first and the result is empty.
Input: ([''],)
Expected Output: ['']
Explanation: Singleton whose only line is the empty string, which is a valid line and is kept once.
Hints
- A duplicate can appear arbitrarily far from its first occurrence, so comparing each line only with the line immediately before it is not enough.
- As you move through the input, what do you need to remember about everything you have already emitted in order to answer 'have I seen this exact line before?' quickly?
- Equality is on the complete string: no trimming, no case folding, and the empty line is an ordinary value that can be kept once.