Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Use DP.

void foo(int n)
{
        static int ind2,ind3,ind5;
        static int arr[30],depth;
        int num2,num3,num5,num,flag=0;
        if(n<=0) return;
        if(depth==0)
        {
                ind2=ind3=ind5=0;// i know this is redundant
                arr[depth++]=1;
        }
        num2=arr[ind2]*2;
        num3=arr[ind3]*3;
        num5=arr[ind5]*5;
 
        num=min(num2,min(num3,num5));
 
        if(num==num2)
        {
                ind2++;
                if(arr[depth-1]!=num2)
                {
                        flag=1;
                        printf("%d\n",arr[depth-1]);
                        arr[depth++]=num2;
                }
        }
        else if(num==num3)
        {
                ind3++;
                if(arr[depth-1]!=num3)
                {
                        flag=1;
                        printf("%d\n",arr[depth-1]);
                        arr[depth++]=num3;
                }
        }
        else
        {
                ind5++;
                if(arr[depth-1]!=num5)
                {
                        flag=1;
                        printf("%d\n",arr[depth-1]);
                        arr[depth++]=num5;
                }
        }
        if(flag)
                foo(n-1);
        else
                foo(n);
        
}

Complete working code: ideone.com/qz4Py

It prints first n ugly numbers.
Note: I have also considered 1 as part of the ugly number series.

- Aashish July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int getKthMagicNumber (int k) {
  queue<int> q2, q3, q5 ;
  int val = 1, minVal ;

  if (k <= 0)
    return 0 ;
  q2.push (2) ;
  q3.push (3) ;
  q5.push (5) ;

  for ( --k ; k > 0 ; -- k ) {
    minVal = min (q2.front(), min (q3.front(), q5.front())) ;
    if ( minVal == q5.front() ) {
      q5.pop () ;
    } else {
      if ( minVal == q3.front() ) {
        q3.pop () ;
      } else {
        q2.pop () ;
        q2.push (minVal*2) ;
      }
      q3.push (minVal*3) ;
    }
    q5.push (minVal*5);
  }
  return minVal ;
}

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

Lookup some combination of "ugly numbers", "hamming numbers" and "dijkstra"

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

<?php
for($i=1;$i<n;$i++)
{
$temp=i;
while($i%2==0)
{
$temp=i/2;
if($temp==1)
break;
}
if($temp==1 || $temp%3==0 || $temp%5==0)
echo $i." ";
}
?>

- Anonymous July 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ignore syntax($).

- Anonymous July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why is it called 'ugly numbers'

- Ankit July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void foo(int n)
{
for (int i =0; i <=n ; i++)
{
int counter = 0;

if (i % 2 == 0)
{
++counter;
}
if ( i % 3 == 0)
{
++counter;
}
if (i % 5 == 0)
{
++counter;
}

if (counter >= 1)
{
if (i % 7 != 0)
printf("%d \n", i);
}

counter = 0;
}

}
int main()
{
int n;

scanf("%d",&n);

foo(n);

return 0;
}

- jae September 10, 2012 | 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