Google VO Interview Question: Map an IP Address to Its City Name

20 Views
No Comments

I have a file with the following format, each line: startIP, endIP, cityName.

Question: Write a function that takes as input an IP address and outputs its associated cityName.

Example:

File format:

startIP, endIP, cityName
1.0.1.1, 1.0.1.10, NYC
1.0.1.20, 1.0.1.30, SF
...

If the input is 1.0.1.9, the output should be NYC.

This is an interval lookup problem: each record maps an IP range <code>[startIP, endIP]</code> to a city name, and the task is to return the city for a given IP address. A common approach is to convert IPs into comparable integers, sort the ranges by start IP, and use binary search to find the range containing the target. For larger datasets, an interval tree or similar indexing structure can also work well. The key points are handling inclusive boundaries correctly and comparing IPs numerically rather than as plain strings.

END
 0