Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
Maintain variables 'count' initialized with 0 and 'num' initialized with 1. Check if num%2 or num%3 or num%5 equals 0. If yes, increment count and then check if count equals K. If yes return num else increment num.
But, here we will get those numbers also which have factors one or more of these numbers and some other prime factor as well. For example, one getting 42 your algo will increment count, which it should not since it is having 7 also as a factor.
if i am not wrong..it seems very straight forward
multiply all 3 prime numbers. and the multiply the result with k. That should be our result.
You couldn't be more wrong here..kth number or for that matter any number in the series here will only have the 3 prime nos as their factor and no other number could be its factor. If you multiply it by k then k becomes a factor of that number which makes it wrong.
I am not telling he is right.. but you explanation is also wrong... in the given example 4 and 8 both is the part of array and 4 is factor of 8..
2 way I can explain..
go through number by number...
check num%p1==0 or num%p2==0 or num%p3==0
increase the counter upto k... our output is the last number...
2nd way is...
keep three variables associated with each prime number.. suppose i,j,k associated initialize with 1. 1 counter also
find min(p1*i,p2*j,p3*k)
increase the corresponding variable increase the counter by 1 until k. give the last number a output..
<pre lang="" line="1" title="CodeMonkey99376" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}
</pre><pre title="CodeMonkey99376" input="yes">
</pre>
<pre lang="" line="1" title="CodeMonkey29742" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
while(true)
system.out.println("amazon is insane to ask such moronic questions"
}
}
</pre><pre title="CodeMonkey29742" input="yes">
</pre>
Look up ugly numbers/hamming numbers on the web.
- Anonymous September 08, 2011