Yahoo Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

if x is ugly, then 2x, 3x and 5x will be ugly.

1. create a min heap(priority Queue) of 2,3 and 5
2. x = pop(heap);
3. insert(heap, 2x)
4. insert(heap, 3x)
5. insert(heap, 5x)

track the variable = size of heap + numbers popped out. When variable count reaches 1500

1. create a max heap
2. 1500thUglyNumber = pop(heap)

- PrateekCaire September 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks.good idea

- Anonymous September 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findNthUgly( int N )
{
    int idx=2;
    int p2,p3,p5;
    
    p2=1,p3=1,p5=1;
    
    int DP[N+2];
    
    DP[1]=1;
    
    while( idx <=N )
    {
           
       DP[idx]=min( DP[p2]*2 , DP[p3]*3 , DP[p5]*5 );
       
       if( DP[idx]==DP[p2]*2 )
        p2++ ;
        
       else if( DP[idx]==DP[p3]*3 )
        p3++ ;
        
       else if( DP[idx]==DP[p5]*5 )
        p5++ ;
        
       
       idx++ ;
       
       }
       
   
   return DP[N] ;
   
}

- JD September 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You will have 3 queues for 2,3 and 5 sets.
Initially
2 -> 2Queue
3 -> 3Queue
5 -> 5Queue

then we will set an index to 0 and start the loop until it gets to 1500.

at each iteration we will check the smallest first number in the queue.
In the first iteration the number we get is from the 2Queueu. That is 2. Our second ugly number is 2 (because first ugly number is 1 :)).
At the second iteration we will multiply each of the numbers in the top of the queue by 2 and enqueue them into the queues.
2*2 -> 2Queue
3*2 ->3Queue
5*2 -> 5Queue

We have the queues like
2Queue = 4
3Queue = 3 , 6
5Queue = 5 , 10

Now we get the smallest element which is 3.

After 1500 iterations we get the 11500th ugly number.

- cac September 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are 21 ugly numbers per 30 numbers (starting from 1). So, to find 1500th ugly number.
Find y = floor(1500/21). Now do z = (y*30). Z will be the (21*y) th ugly number. Now in constant time you can find the 1500th ugly number (maximum 21 steps).

- sundar September 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

from 1 to 30 I think the set of NON ugly numbers would comprise of :- {7,11,13,14,17,19,22,23,26,29}
rest of will be Ugly numbers..

and Non ugly numbers are 10 ..hence
out of first 30 there are 20 ugly numbers ..

Please explain your answer .

- Aditya October 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u missed out 1 coz 1 is also ugly number.

- raj December 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is an ugly number?

- cirus November 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

An ugly number is a number that has only 2, 3 or 5 as prime factors, i.e any number that can be represented as 2^x * 3^y * 5^z where x,y and z are positive integral values. Note that this definition allows for 1 to be an ugly number for x=0, y=0 and z=0.

- chandransuraj February 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>

typedef unsigned long long big_int;

unsigned long long FindNthUglyNumber(int n)
{
	vector<big_int> uglyNumber;
	int i, j, k, m;
	i = j = k = m = 0;

	uglyNumber.push_back(1);

	for(;;)
	{
		while(uglyNumber[i]*2 <= uglyNumber[m])
			i++;
		while(uglyNumber[j]*3 <= uglyNumber[m])
			j++;
		while(uglyNumber[k]*5 <= uglyNumber[m])
			k++;

		if(uglyNumber[i]*2 < uglyNumber[j]*3 && uglyNumber[i]*2 < uglyNumber[k]*5)
			uglyNumber.push_back(uglyNumber[i++]*2);
		else if(uglyNumber[j]*3 < uglyNumber[k]*5)
			uglyNumber.push_back(uglyNumber[j++]*3);
		else
			uglyNumber.push_back(uglyNumber[k++]*5);

		m++;
		if(m >= (n-1))
			break;
	}

	return uglyNumber.back();
}

- Anonymous July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ans is : 13668750000

- Dipankar December 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sabaassss

- Aditya July 27, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More