Microsoft Interview Question for Software Engineers


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;

          list.add(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?

- aonecoding June 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Sourav July 23, 2021 | Flag
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;

                    list.Add(i);
                }
            }

            if (n > 10)
            {
                return 0;
            }

            int result = 0;
            list.Reverse();
            
            foreach (var i in list)
            {
                result = result * 10 + i;
            }

            return result;
        }

- Chander Dhall July 08, 2018 | Flag Reply
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;

                    list.Add(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)

print("the answer is ", value);
value = smallestFactors(72)

print("the answer is ", value);

- me@chanderdhall.com July 08, 2018 | Flag Reply
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;
}

- dr. hfuhruhurr June 06, 2018 | Flag Reply
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;
}

- dr. hfuhruhurr June 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- manjeet.rulhania June 06, 2018 | Flag Reply
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
	{
		// your code goes here
		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.

- Anonymous June 07, 2018 | Flag Reply
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.

- Anonymous June 07, 2018 | Flag Reply
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

- pasio June 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Decompose n to its prime factors. Sort the factors.

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

What should be answer for n=26

- Suresh June 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what should be answer for n=26

- Suresh June 22, 2018 | Flag Reply
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 number=27, answer = 0;
    int i=9;
    while(i > 1)
    {
        while(number%i == 0)
        {
            answer = answer*10 + i;
            number = number/i;
        }
        i--;
    }
    
    if(number > 1)
        cout<<"Not feasible";
    else
        cout<<reverse(answer); //reverse the digits
    return 0;
}

- Brandy June 24, 2018 | Flag Reply
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;
        }

- Jahnavi September 06, 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