Google Interview Question for Software Engineer / Developers






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

Before posting a question, atleast UNDERSTAND it first. Your output should be something like (first 20):
1
2
4
5
8
10
16
20
25
32
40
50
64
80
100
125
128
160
200
250

- anonymous May 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think there is a tricky way. Let 5 = 2^x, where x ~= 2.2 (I didn't solve it by computer. We say it is x = 2.2).
Therefore, 2^i*5^j = 2^i * (2^2.2)^j = 2^(i+2.2j). The problem can be reduced: increase i, j to sort the outputs. Then, it will become an "counting problem."

j=0, outputs are 1, 2, 3, 4, 5, 6, 7, 8, 9......
j=1, outputs are 2.2, 3.2, 4.2, 5.2, .....
j=2, outputs are 4.4, 5.4, 6.4, 7.4, .....
.
.
.

Tackling the sorting problem like this will be easier since it has a regular pattern (the incremental is 1 for each of j's).

You are welcome to comment on it. Thank you.

- cyubing June 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

above sequence is arbit....i thnk once i or j is incrmented it cant b decremnted...we hv to keeep on incrmntng it.....othrwise give me a soln i m getng no idea

- mm May 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@mm: learn FIRST how to type first, before go for job hunting. What you mean by "arbit" ? Don't you know the spelling of "decremented" or "incrmntng"? Get lost!

For others: there exists O(n) solution for this problem, first proposed by Dijstra. It's a variation of "Ugly Number" that generates sequence of the form 2^i 3^j 5^k.

- anonymous May 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

An interesting problem, which looks simple, but has deep root in number theory and applications in various fields. en.wikipedia.org/wiki/Regular_number

- Anonymous May 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@mm ... if i,j should always be incrementing.. the what is 2*5 doing after 2^3 ????

- anand May 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Shortest way to arrange in sorted order */
int i=0, j=0, x=0, count;
for(count=1;count<=20;count++)
{
printf("%d",(pow(2,x)*pow(5,j)));
if(x==0 || x==1)
{ x=++i;j=0; }
else
{ x-=2; j++ }
}

- Vikash Tibrewal May 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How did you arrive at this algorithm? I ran your code and it worked, but I still couldn't figure out a pattern. The only thing I noticed was the exponent for the second term (5) alternates between 0 and 1...the exponent for the first term (2) seems a little arbitrary.

- Anonymous May 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

5=2*2+1
so multiply 2 twice, then the next is to divide by 4 then multiply 5

- Anonymous May 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I just tried to run it little longer and sequence breaks on 26th iteration:
...
250
256
320
400
500 <---
625 <---
512 <---
640
800
...

- Vadym June 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Geeks have a look you can get the idea here

shashank7s dot blogspot dot com/2011/05/wap-to-generate-series-of-ugly-or-lucky dot html

or type on Google query "Cracking The Code Shashank" above link

- WgpShashank May 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well Here is the Running Code But Until & Unless You Will Vist Above Blog it won't make you more clear.


#include<stdio.h>
#include<iostream>
using namespace std;
#define k 20 //modify it

int Lucky[k], ptr2, ptr5;

int minimum(int x,int y)
{
if (x<y) return x;
return y;
}

int nextLucky(int num)
{
int num2,num5;
if(num==0)
{
Lucky[num]=1;
ptr2=ptr5=0;
return Lucky[num];
}

num2=Lucky[ptr2]*2;
num5=Lucky[ptr5]*5;

Lucky[num]=minimum(num2,num5);

if(Lucky[num]==num2) ptr2++;
if(Lucky[num]==num5) ptr5++;

return Lucky[num];
}

int main()
{
int i;

for(i=0; i<k; i++)
printf("Lucky number no. %d is %d\n", i+1,nextLucky(i));
}

WgpShashank

- WgpShashank May 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nicely done!!!

- rahul bawa June 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

minHeap.insert(1);
while(1)
{
minHeap.getMin(min);
print(min);

minHeap.add(min*2);
minHeap.add(min*5);
}

- try_catch May 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Who will take care of duplicates?

Learn to work on paper first, before posting a solution here!

- What a dumb asshole! May 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

need a spoon feed?

Overload insertion for avoiding duplicates.

- try_catch May 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

check this -> geeksforgeeks.org/?p=753

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

ano

- mc June 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just have 2 queues to implement this question

- shiqing881215 August 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

print the first 4 elements

cout <<2^0, 2^1, 2^2, 5^1

int nextI = 2, nextJ = 1;

int printedTillNow = 4;

while ( printedTillNow < n) {
nextI++;
if( Pow(2,nextI) > Pow(5,nextJ) {
nextJ++;
}
int j = 0;
for ( int i = nextI; i>=0; i-=2) {
cout<< Pow(2,i)*Pow(5,j);
printedTillNow++;
j++;
if(j > nextJ) {
j = 0;
}
}
}

- Anonymous January 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
    // Generate the first n numbers
    const int n = 20;

    std::vector<int> v(n);
    v[0] = 1;

    int i2 = 0; // Index for 2
    int i5 = 0; // Index for 5

    // Next two candidates
    int x2 = 2 * v[i2];
    int x5 = 5 * v[i5];

    for (int i = 1; i != n; ++i)
    {
        int m = std::min(x2, x5);

        std::cout << m << " ";

        v[i] = m;

        if (x2 == m)
        {
            ++i2;
            x2 = 2 * v[i2];
        }
        if (x5 == m)
        {
            ++i5;
            x5 = 5 * v[i5];
        }
    }

    std::cout << std::endl;

    return 0;
}

- ak March 18, 2013 | Flag Reply


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