## Microsoft Interview Question for Software Engineers

• 2

Country: United States

Comment hidden because of low score. Click to expand.
3
of 5 vote

Looking for interview experience sharing and coaching?

Visit AONECODE.COM for ONE-ON-ONE private lessons by FB, Google and Uber engineers!

SYSTEM DESIGN
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algorithms, Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Get hired from G, U, FB, Amazon, LinkedIn, Yahoo and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!

SOLUTION:

``````int smallestFactors(int n) {
if (n < 10) {
return n;
}
List<Integer> list = new ArrayList<>();
for (int i = 9; i > 1; i--) {
while ((n % i) == 0) {
n /= i;
}
}
if (n > 10) {
return 0;
}
int result = 0;
for (int i : list) {
result = result * 10 + i;
}
return result;
}``````

followup: what if we need to worry about the integer overflow?

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

what is n=169, it should return 1313 i guess.

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

I just tested this. This works perfectly. Other solutions are giving 94 and 98 as answers. This solution correctly gets you 49 and 89.

``````static int smallestFactors(int n)
{
if (n < 10)
{
return n;
}

List<int> list = new List<int>();

for (int i = 9; i > 1; i--)
{
while ((n % i) == 0)
{
n /= i;

}
}

if (n > 10)
{
return 0;
}

int result = 0;
list.Reverse();

foreach (var i in list)
{
result = result * 10 + i;
}

return result;
}``````

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

This is the correct solution (in C#)

``````static int smallestFactors(int n)
{
if (n < 10)
{
return n;
}

List<int> list = new List<int>();

for (int i = 9; i > 1; i--)
{
while ((n % i) == 0)
{
n /= i;

}
}

if (n > 10)
{
return 0;
}

int result = 0;
list.Reverse();

foreach (var i in list)
{
result = result * 10 + i;
}

return result;``````

}

This is the correct solution in Python 3

``````def smallestFactors(n):
list = []
if n < 10:
return n
for i in range(9, 1, -1):
while 0 == n % i:
n = n / i
list.append(i)
if n > 10:
return 0
result = 0
for i in reversed(list):
result = result * 10 + i;
return result

value = smallestFactors(36)

value = smallestFactors(72)

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

``````unction smallest_factors(n) {
let result = `no result found for \${n}`,
product;

if( n < 10 ) return n;

for(let i=+n+1; i<Number.MAX_SAFE_INTEGER; i++) {
const sa = (''+i).split('');
//console.log(sa);
product = sa.reduce((acc,cur) => {
//console.log(`entered reduce: acc=\${acc}, cur=\${cur}`);
acc *= +cur;
//console.log(`leaving reduce: acc=\${acc}, cur=\${cur}`);
return acc;
}, 1);

//console.log(`product=\${product}`);
if( n == product ) {
result = i;
break;
}
}
return result;
}``````

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

``````unction smallest_factors(n) {
let result = `no result found for \${n}`,
product;

if( n < 10 ) return n;

for(let i=+n+1; i<Number.MAX_SAFE_INTEGER; i++) {
const sa = (''+i).split('');
product = sa.reduce((acc,cur) => {
acc *= +cur;
return acc;
}, 1);

if( n == product ) {
result = i;
break;
}
}
return result;
}``````

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

for n = 36, shouldn't be m = 49 instead of 66 ?

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

``````import java.util.*;
import java.lang.*;
import java.io.*;
class test
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=n+1;i<2*n;i++){
int result=1;
int t=i;
while(t>0){
result=result*(t%10);
t=t/10;
}
if(result==n){
System.out.println(i);
break;}

}

}
}``````

if numbers are prime it will not print any thing.

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

for n = 36, m = 66 is wrong. m = 49 is smaller than 66.

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

``````def get_smallest_int_made_of_factors(num):
factors = []
for digit in xrange(9, 1, -1):
while num % digit == 0:
factors.append(digit)
num /= digit
return int("".join([str(digit) for digit in reversed(factors)])) if num == 1 else None``````

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

Decompose n to its prime factors. Sort the factors.

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

``What should be answer for n=26``

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

what should be answer for n=26

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

Divide the number recursively with the largest digit possible (9,8,...,2). Save the digit at each step.

``````int reverse(int& value)
{
int reverseNum = 0;
while(value)
{
int digit = value % 10;
reverseNum = reverseNum*10 + digit;
value = value/10;
}
return reverseNum;
}

int main(int argc, const char * argv[]) {
int i=9;
while(i > 1)
{
while(number%i == 0)
{
number = number/i;
}
i--;
}

if(number > 1)
cout<<"Not feasible";
else
return 0;
}``````

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

``````static int smallestFactors(int n)
{
if (n < 10)
{
return n;
}

int result = 0, mul = 1;
for (int i = 9; i > 1; i--)
{
while ((n % i) == 0)
{
n /= i;

result = result == 0 ? i : i * mul + result;
mul = mul * 10;
}
}

if (n > 10)
{
return 0;
}

return result;
}``````

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.

### 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.