Hot answers tagged

16

I'm being extra pedantic as this is a job interview question. Your code is better than 90% of what I have seen in job interviews. It shows that you have a fairly good understanding of how to solve the problem but there are improvements to be made in both minor details and the overall design of the solution. (This answer is a bit unstructured as I just make ...


15

The maximum int value is \$2^{31}-1\$, so your BitSets have \$2^{31}-1\$ entries each, so together they only cover \$2^{32}-2\$ IPs, missing two. And you documented their ranges incorrectly. If the file contains 255.255.255.255, you crash like this: Exception in thread "main" java.lang.IndexOutOfBoundsException: bitIndex < 0: -2147483648 at ...


8

I'm agree with TorbenPutkonen's considerations about your code and with Manuel's observation about edge ipaddresses cases like 255.255.255.255 that can break your code, I focused about your method toLongValue translating your ipv4 address to an unique long: static long toLongValue(String ipString) { StringBuilder field = new StringBuilder(3); int ...


7

Since it's a job interview question, you probably want to: specify a package instead of using the default one. E.g. package chpter.one; put only the necessary code (BitSetUniqueIpCounter.java, IpCounterApp.java, UniqueIpCounter.java) in src/main/java. Anything that is only used for tests should be in src/test/java or src/test/resources/. Don't forget to ...


5

I see one little problem with the code you gave, even though I feel like it's a pretty good take at the problem you have. Best case/worst case, you always ask for 512Mb memory. Say I give you a file that has 1 million times the same address, the code still initialized a 514 megabytes array. Instead of using an array structure, I believe a tree structure ...


4

consuming as little memory and time as possible While your one bit per possible address is decent, it's not as little as possible. I actually have to run Java with -Xmx to allow it to take more heap space than usual. An alternative idea: Go through the file, parse the addresses, and store them in temporary files. For example the address 1.2.3.4 goes into ...


Only top voted, non community-wiki answers of a minimum length are eligible