Microsoft Interview Question for Software Engineer / Developers






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

void NumSum(int targetSum, vector<int> v, int starVal, int partialSum)
{
if (partialSum == targetSum)
{
printf( "{");
for (int i=0; i < v.size(); i++)
{
printf ("%d , ", v[i]);
}
printf( "}");
return;
}
for (int i = startVal; i < targetSum;i++)
{
if (i+partialSum <= targetSum)
{
NumSum(targetSum, v.push(i), i, partialSum+i);
v.pop_back();
}
}
}

void NumberofSum(int n)
{
vector <int> v;
for (int i=1; i < n;i++)
{
printf("{");
NumSum(i , v, 1, 0);
printf("}\n");
v.clear();
}
}

- r.ramkumar December 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void NumSum(int targetSum, vector<int> v, int starVal, int partialSum)
{
     if (partialSum == targetSum)
     {
        printf( "{");
        for (int i=0; i < v.size(); i++)
        {
            printf ("%d , ", v[i]);
        } 
        printf( "}");
        return;
     }
     for (int i = startVal; i < targetSum;i++)
     { 
          if (i+partialSum <= targetSum)
          {
             NumSum(targetSum, v.push(i), i, partialSum+i);
             v.pop_back();
          }
     }
}

void NumberofSum(int n)
{
     vector <int> v;
     for (int i=1; i < n;i++)
     {
       printf("{");
       NumSum(i , v, 1, 0);  
       printf("}\n");
       v.clear();
     }
}

- r.ramkumar December 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sweeeet !! U r hired !! :)

Actually this was what he was looking , i had written some what similar code like this but i had hard time figuring out the recursive calling part of it , In the end he said he was kind of glad that i reached there but u know actually i took more than 30mins to get that incomplete code .

Anyways above code works it has some typos so here it is without those typos and it works in VS 2008

void NumSum(int targetSum, vector<int> v, int startVal, int partialSum)
{
     if (partialSum == targetSum)
     {
        printf( "{");
        for (int i=0; i < v.size(); i++)
        {
            printf ("%d , ", v[i]);
        } 
        printf( "}");
        return;
     }
     for (int i = startVal; i < targetSum;i++)
     { 
          if (i+partialSum <= targetSum)
          {
			 v.push_back(i);
             NumSum(targetSum, v , i, partialSum+i);
             v.pop_back();
          }
     }
}

void NumberofSum(int n)
{
     vector <int> v;
     for (int i=1; i <= n;i++)
     {
       printf("{");
       NumSum(i , v, 1, 0);  
       printf("}\n");
       v.clear();
     }
}

- sachin323 December 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

isn't the vector supposed to be passed by reference as below?
void NumSum(int targetSum, vector<int> &v, int startVal, int partialSum)

- anonymousse December 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

HI Sachin,

How did you get more than 1 occurence of number say for 2={1,1} how did u able to get 2 1's ???

- Anonymous December 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am sure that this is not exactly what they were looking for. To solve this problem, the best way is to use dp.

- fentoyal January 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sachin, nice code

- siva.sai.2020 February 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the tested correct code.
Please correct me if i m wrong.

//no. of ways a number can be formed by the sum of +ve no.s less than it
import java.util.Scanner;
class NoOfWays
{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter the number");
		int n=sc.nextInt();
		int ways=findWays(n,n-1,"");
		System.out.println("No. of ways are "+ways);
	}
	static int findWays(int n, int denom,String s)
	{
		if(n<0 || denom <=0)
			return 0;
		else if(denom==1)
		{
			for(int i=0;i<n;i++)
			s="1 + "+s;
			System.out.println(s.substring(0,s.length()-2));
			return 1;
		}
		else if(n==0)
		{
			System.out.println(s.substring(0,s.length()-2));
			return 1;
		}
		int ways=0;
		for(int i=0; i*denom<=n;i++)
		{
			String s2=s;
			for(int j=0;j<i;j++)
			{
				s2=denom+" + "+s2;
			}
			ways+=findWays(n-i*denom,denom-1,s2);
		}
		return ways;
	}
}

- Anonymous December 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <algorithm>

using namespace std;

void allSumTo(int n, vector<int> sum) {
  if (n == 0)  {
    copy(sum.begin(), sum.end(), ostream_iterator<int>(cout, " ")); cout << endl;
  } else {
    for (int i = 1; i <= n; ++i) {
      int threshold = (sum.empty() ? 0 : sum.back());
      if (i >= threshold) {
        sum.push_back(i);
        allSumTo(n - i, sum);
        sum.pop_back();
      }
    }
  }
}

int main(int argc, char** argv) {
  int n = 4;
  if (argc > 1) n = atoi(argv[1]);
  vector<int> sum;
  allSumTo(n, sum);
  return 0;
}

- maxxum7 December 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

GetAllSum(4, 1, new List<int>(), 0);;

static void GetAllSum(int target, int currentMin, List<int> output, int partialSum)
{
// Recursion end condition
if (target == partialSum)
{
Console.WriteLine();
foreach (int i in output)
{
Console.Write(i + " ");
}
}
else
{
// Start adding digits from last currentMin, once you have used 2 in your ouput you shouldn't use 1 again... This is to prevent outputs like 1,2,1 since they are equal to 1,1,2
for (int start = currentMin; start < target; start++)
{
if (partialSum + start <= target)
{
output.Add(start);
// Keep adding the current Min to partial Sum...
GetAllSum(target, start, output, partialSum + start);
output.RemoveAt(output.Count - 1);
}
}
}
}

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

Using Greedy Algorithm :

//
// main.c
// NumberOfSum
//
// Created by Srikant Aggarwal on 25/11/11.
// Copyright 2011 NSIT. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

struct stack_element
{
int data;
struct stack_element *next;
};

typedef struct stack_element stack_element;

stack_element *top;

void push(int d)
{
stack_element *temp;

temp = (stack_element *)malloc(sizeof(stack_element));

temp->data = d;
temp->next = top;

top = temp;
}

int pop()
{
int x = -1;

if(top != NULL)
{
x = top -> data;

stack_element *temp = top;
top = top -> next;

free(temp);
temp = NULL;
}

return x;
}

void peek_and_print_stack()
{
if(top == NULL)
return;
else
{
int x = pop();
peek_and_print_stack();
printf(" %d ", x);
push(x);
}
}

void num_of_sum(int n)
{
top = NULL;
int num = n;
int sum = 0;
int x;

while(1)
{
push(num);
sum += num;

if(sum == n)
{
printf("\n");
peek_and_print_stack();
do
{
x = pop();

if(x == -1)
{
return;
}

sum = sum - x;
}while(x == 1);

num = x-1;
}
else if(sum > n)
{
x = pop();
sum = sum - x;
num = x-1;
}

};
}

int main (int argc, const char * argv[])
{

// insert code here...
int num;

printf("Enter no = ");
scanf("%d", &num);

num_of_sum(num);
}

- Srikant Aggarwal November 25, 2011 | 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