robertlaub
BAN USER- -1of 3 votes
AnswersWrite a function which returns the number of times the digit "1" appears in a number which is generated from raising 11 to the Nth power where N is passed in as an input parameter. The range of N is 0 to 1,000.
Be sure to unit test your solution.
For instance, If N is 3, the number is 1331 and the function returns 2.
If N is 5, the function returns 3.
If N is 10, the function returns 1 and so on.public int solution(int N) { ... }
You have 30 minutes to complete the problem.
- robertlaub in United States| Report Duplicate | Flag | PURGE
iCIMS Senior Software Development Engineer Java
For N== 3, solution(3) returns 1 which is clearly wrong.
Also, factorial(35) throws an ArithmeticException.
It fails at N equal to 19:
N = 19, Power = 6.115909044841455E19, findNumTimes1Appears() = 0
You can save me some time checking your answers by writing some test cases. Try these for starters:
for (int i = 0; i < 49; ++i) {
System.out.println("N = " + i + ", Power = " + Math.pow(11, i) + ", findNumTimes1Appears() = " + findNumTimes1Appears(i));
}
int N;
N = 50; System.out.println("N = " + N + ", Power = " + Math.pow(11, N) + ", findNumTimes1Appears() = " + findNumTimes1Appears(N));
N = 100; System.out.println("N = " + N + ", Power = " + Math.pow(11, N) + ", findNumTimes1Appears() = " + findNumTimes1Appears(N));
N = 200; System.out.println("N = " + N + ", Power = " + Math.pow(11, N) + ", findNumTimes1Appears() = " + findNumTimes1Appears(N));
Also notice that for N=300, the pow() function overflows.
- robertlaub April 18, 2015Your solution appears to fail at N = 11. Your code above finds the number of 1's to be 1 but it is obviously 4. Did you unit test?
N = 11, Power = 2.85311670611E11, findNumTimes1Appears(11) = 1
Could you provide this code in Java?
- robertlaub April 20, 2015