Remove Duplicates While Preserving First-Appearance Order
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Return each array value once in its original first-appearance order, using membership tracking without sorting the result.
Constraints
- `data` is always an array, never null.
- `0 <= len(data) <= 200000`.
- Values range from `-1000000000` through `1000000000`.
- Equal values count as duplicates even when separated by other values.
- Do not sort the result; it must preserve first-occurrence order.
- Aim for expected `O(n)` time.
- Values are integers (a portable practice interface); every value fits in a signed 32-bit integer.
Examples
Input: ([],)
Expected Output: []
Explanation: Minimum valid input: an empty array has no values to keep, so the result is empty.
Input: ([5],)
Expected Output: [5]
Explanation: Singleton: the only element is its own first occurrence.
Hints
- Traverse `data` once in its original order and decide about each value at the moment you first encounter it.
- Tracking which values have appeared before and building the ordered result are two different responsibilities; one structure does not have to do both.
- Re-scanning the output already built for every new element is correct but does not reach the expected O(n) time the contract asks for.