Interview Question


Country: United States




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

There is no middle of anything when we are looking at a number. A number, rather more precisely integer is given by this regex (ignoring sign) :

N -> [0-9]+

That would mean, in a context free way, it can be written as :

N --> [0-9]* [0-9]? [0-9]*

That is how you should define middle. In this definition, of course middle becomes meaningless. Example: 34543 . What is the middle? My grammar of course is terribly ambiguous, but hope I did make the point.

Now, of course there are two problems.
1. Get all the factors of a number. This is not very interesting problem.
[ geeksforgeeks.org/find-divisors-natural-number-set-1/ ]
2. A predicate to declare if a number is a special number. That is slightly more interesting.
But then fairly trivially done by (with proper collection minus and subset definition ):

special_digits = '356'.toCharArray
all_digits = ['0':'9'].string.toCharArray 
drop_digits = all_digits - special_digits

def is_special_no( n ){
  // set minus : to get the left over digits after subtracting drop_digits
  left =  set ( str(n).toCharArray ) - drop_digits
  // when left is non empty and is a proper subset of special_digits then true 
  !empty(left) && left <= special_digits
}

println( is_special_no(104) )

But the bigger question is, why write this way? It is terribly un-optimal !Clearly there are better way to do so? As it turned out, there is!!

special_digits = set ( '356'.toCharArray )
def is_special_no( n ){
  !empty ( select ( str( n ).toCharArray ) where { $.o @ special_digits } )
}

println( is_special_no(134) )

Now this is cleaner, neater, and optimal. Now it is trivial to generate the appropriate imperative code.

- NoOne March 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{
long long num,count=0;int i;
scanf("%lld",&num);
for(i=2;i*i<=num;i++)
{
if(num%i==0)
{
int factor2=num/i;
int k=i;
while(k)
{
int t=k%10;k/=10;
if(t==3||t==5||t==6){count++;break;}
}
if(k!=factor2)
{
while(factor2)
{
int t=factor2%10;factor2/=10;
if(t==3||t==5||t==6){count++;break;}
}
}
}
}
while(num)
{
int t=num%10;
num/=10;
if(t==3||t==5||t==6){count++;break;}
}
printf("%lld\n",count);

}

- Anubhav kumar March 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{
long long num,count=0;int i;
scanf("%lld",&num);
for(i=2;i*i<=num;i++)
{
if(num%i==0)
{
int factor2=num/i;
int k=i;
while(k)
{
int t=k%10;k/=10;
if(t==3||t==5||t==6){count++;break;}
}
if(k!=factor2)
{
while(factor2)
{
int t=factor2%10;factor2/=10;
if(t==3||t==5||t==6){count++;break;}
}
}
}
}
while(num)
{
int t=num%10;
num/=10;
if(t==3||t==5||t==6){count++;break;}
}
printf("%lld\n",count);

}

- Anubhav kumar March 08, 2018 | 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