We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I usually post the most up-to-date solutions in my HackerRank solutions. You can cite that if you like, any citation method is fine, I wasn't even expecting one, so thanks.
The code uses a long instead of an int to avoid integer overflow. The maximum value of an int is about 2 billion. The exact value can be respresented in the code as Integer.MAX_VALUE. Any time you multiply a lot of numbers together, you must be careful of avoiding integer overflow. This was a actually a pitfall that my friend came across in an interview.
Since we can have 10000 nodes (according to the problem constraint) where each node can have value up to 1000, multiplying these numbers can easily exceed ~2 billion. Using a long lets us store these larger numbers. If for some reason a long is not big enough, you can use a BigInteger, which is loads of fun and can store numbers of any size.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Visitor Pattern
You are viewing a single comment's thread. Return to all comments →
I usually post the most up-to-date solutions in my HackerRank solutions. You can cite that if you like, any citation method is fine, I wasn't even expecting one, so thanks.
The code uses a long instead of an int to avoid integer overflow. The maximum value of an int is about 2 billion. The exact value can be respresented in the code as Integer.MAX_VALUE. Any time you multiply a lot of numbers together, you must be careful of avoiding integer overflow. This was a actually a pitfall that my friend came across in an interview.
Since we can have 10000 nodes (according to the problem constraint) where each node can have value up to 1000, multiplying these numbers can easily exceed ~2 billion. Using a long lets us store these larger numbers. If for some reason a long is not big enough, you can use a BigInteger, which is loads of fun and can store numbers of any size.