Basically a 'first unique character' problem but with IPs instead of chars.
Clarify the constraints (streaming vs. stored, memory limits, IP format) and then propose a solution using a hash map to count occurrences while tracking the order of first appearance. For a streaming scenario, maintain a doubly linked list of unique IPs and a hash map for O(1) updates, ensuring the first unique IP is always at the head.
Pro tip: Mention that you would handle IPv4 and IPv6 uniformly by treating IPs as strings or using a trie, and discuss how to scale with distributed counting if the stream is huge.
Ask about the nature of the stream (real-time vs. batch), memory constraints, and whether the IP addresses are IPv4, IPv6, or both. Confirm if we need to return the first unique IP in the order of arrival.
For a static list, a hash map counting frequencies and a second pass to find the first with count 1 works. For streaming, use a hash map to store nodes of a doubly linked list, where the list maintains unique IPs in order of first appearance.
Initialize an empty hash map and an empty doubly linked list. For each incoming IP, if it's not in the map, add it to the tail of the list and store the node in the map; if it's already in the map, remove its node from the list (if present) and mark it as seen multiple times.
Consider cases where no unique IP exists, the stream is empty, or memory is insufficient. Discuss how to handle duplicates that appear after being removed from the list.
Explain that each IP is processed in O(1) time on average, with O(U) space where U is the number of unique IPs seen so far. The first unique IP is always at the head of the list, allowing O(1) retrieval.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.