Find the First Unique IP
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency with streaming data handling, stateful data structures, and algorithmic design for identifying unique elements in chronological event streams, and is categorized under Coding & Algorithms.
Constraints
- 0 <= len(operations) == len(values) <= 200000
- Each operation is either "recordHit" or "getFirstUniqueIp"
- For "recordHit", the IP string length is between 1 and 45 characters
- The expected solution should run in O(n) total time across all operations
Examples
Input: (["recordHit", "recordHit", "recordHit", "getFirstUniqueIp", "recordHit", "getFirstUniqueIp", "recordHit", "getFirstUniqueIp"], ["10.0.0.1", "10.0.0.2", "10.0.0.1", None, "10.0.0.2", None, "10.0.0.3", None])
Expected Output: ["10.0.0.2", None, "10.0.0.3"]
Explanation: After the first three hits, only 10.0.0.2 is unique. After 10.0.0.2 appears again, no unique IP remains. After 10.0.0.3 is added, it becomes the first unique IP.
Input: ([], [])
Expected Output: []
Explanation: No operations means there are no query results.
Input: (["getFirstUniqueIp", "getFirstUniqueIp"], [None, None])
Expected Output: [None, None]
Explanation: There are no recorded hits, so every query returns None.
Input: (["recordHit", "getFirstUniqueIp", "recordHit", "getFirstUniqueIp"], ["192.168.0.1", None, "192.168.0.1", None])
Expected Output: ["192.168.0.1", None]
Explanation: The IP is unique after the first hit, but not after it appears a second time.
Input: (["recordHit", "recordHit", "recordHit", "recordHit", "getFirstUniqueIp", "recordHit", "getFirstUniqueIp"], ["1.1.1.1", "2.2.2.2", "1.1.1.1", "3.3.3.3", None, "2.2.2.2", None])
Expected Output: ["2.2.2.2", "3.3.3.3"]
Explanation: After 1.1.1.1 repeats, 2.2.2.2 becomes the earliest unique IP. After 2.2.2.2 repeats, 3.3.3.3 becomes the earliest unique IP.
Hints
- A hash map can track how many times each IP has appeared.
- You also need to preserve the order of candidate unique IPs. Consider a queue/deque with lazy cleanup from the front.