Microsoft Interview Question for Software Engineer / Developers


Team: QTB
Country: India
Interview Type: In-Person




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

the topdown approach.. recursive with global function and global storage structure..

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
float a[100][100]={0};
int l=0;
void fill(float n,int i,int j)
{
	l=max(l,i);
	if(a[i][j]>=n)
	{a[i][j]-=n; }
	else
	{
		n-=a[i][j];
		a[i][j]=0;
		if (n>0)
		{
			fill(n/2,i+1,j);
			fill(n/2,i+1,j+1);
		}
	}
}
int main()
{
	for(int i=0;i<100;i++)
		for(int j=0;j<=i;j++)
			a[i][j]=1;
	float n; cin >>n;
	fill(n,0,0);
	for(int i=0;i<=l+2;i++)
	{
		for(int j=0;j<=i;j++)
		{
			cout<<a[i][j]<<"\t";
		}
		cout<<"\n";
	}
	return 0;
}

- dhandeepm March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The question is not clear to me. Can someone clarify.
e.g.
Let us say the stack Is like this
v00
v10 v11
v20 v21 v22

I am guessing, v00 fills in first.
In which scenario will water flow from v00 to v01 or some place else

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

At any nth cup it will be filled by it's parent

i.e.

T(n) = T(P1(n) ) +T(P2(n))

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

You can maintain an int[ , ] array and use recursion to simulate the process, the signature of the method can be like this (in c#): void Process(ref int[ , ] matrix, int c, int v, int x, int y). c is the volume of water that would fill in this cup dropping from its parent(s). v is the volume of this cup. The logic is as follows:
1. if x or y is out of bound of matrix, return. (means the water will drop on the floor)
2. get the current empty volume of this cup (int empty=v-matrix[x,y])
3. fill the cup with c: matrix[x,y] += ( c>empty ) ? empty : c;
4. if matrix[x,y]==v do 5 and 6
5. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y);
6. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y+1);

Finally we will get a matrix with numbers indicating the volumes of water of all cups. We can easily get the answer for all 3 questions above.

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can maintain an int[ , ] array and use recursion to simulate the process, the signature of the method can be like this (in c#): void Process(ref int[ , ] matrix, int c, int v, int x, int y). c is the volume of water that would fill in this cup dropping from its parent(s). v is the volume of this cup. The logic is as follows:
1. if x or y is out of bound of matrix, return. (means the water will drop on the floor)
2. get the current empty volume of this cup (int empty=v-matrix[x,y])
3. fill the cup with c: matrix[x,y] += ( c>empty ) ? empty : c;
4. if matrix[x,y]==v do 5 and 6
5. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y);
6. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y+1);

Finally we will get a matrix with numbers indicating the volumes of water of all cups. We can easily get the answer for all 3 questions above.

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can maintain an int[ , ] array and use recursion to simulate the process, the signature of the method can be like this (in c#): void Process(ref int[ , ] matrix, int c, int v, int x, int y). c is the volume of water that would fill in this cup dropping from its parent(s). v is the volume of this cup. The logic is as follows:
1. if x or y is out of bound of matrix, return. (means the water will drop on the floor)
2. get the current empty volume of this cup (int empty=v-matrix[x,y])
3. fill the cup with c: matrix[x,y] += ( c>empty ) ? empty : c;
4. if matrix[x,y]==v do 5 and 6
5. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y);
6. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y+1);

Finally we will get a matrix with numbers indicating the volumes of water of all cups. We can easily get the answer for all 3 questions above.

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can maintain an int[ , ] array and use recursion to simulate the process, the signature of the method can be like this (in c#): void Process(ref int[ , ] matrix, int c, int v, int x, int y). c is the volume of water that would fill in this cup dropping from its parent(s). v is the volume of this cup. The logic is as follows:
1. if x or y is out of bound of matrix, return. (means the water will drop on the floor)
2. get the current empty volume of this cup (int empty=v-matrix[x,y])
3. fill the cup with c: matrix[x,y] += ( c>empty ) ? empty : c;
4. if matrix[x,y]==v do 5 and 6
5. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y);
6. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y+1);

Finally we will get a matrix with numbers indicating the volumes of water of all cups. We can easily get the answer for all 3 questions above.

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can maintain an int[ , ] array and use recursion to simulate the process, the signature of the method can be like this (in c#): void Process(ref int[ , ] matrix, int c, int v, int x, int y). c is the volume of water that would fill in this cup dropping from its parent(s). v is the volume of this cup. The logic is as follows:
1. if x or y is out of bound of matrix, return. (means the water will drop on the floor)
2. get the current empty volume of this cup (int empty=v-matrix[x,y])
3. fill the cup with c: matrix[x,y] += ( c>empty ) ? empty : c;
4. if matrix[x,y]==v do 5 and 6
5. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y);
6. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y+1);

Finally we will get a matrix with numbers indicating the volumes of water of all cups. We can easily get the answer for all 3 questions above.

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can maintain an int[ , ] array and use recursion to simulate the process, the signature of the method can be like this (in c#): void Process(ref int[ , ] matrix, int c, int v, int x, int y). c is the volume of water that would fill in this cup dropping from its parent(s). v is the volume of this cup. The logic is as follows:
1. if x or y is out of bound of matrix, return. (means the water will drop on the floor)
2. get the current empty volume of this cup (int empty=v-matrix[x,y])
3. fill the cup with c: matrix[x,y] += ( c>empty ) ? empty : c;
4. if matrix[x,y]==v do 5 and 6
5. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y);
6. recursive call: Process(ref matrix, (c-empty)/2, v, x+1, y+1);

Finally we will get a matrix with numbers indicating the volumes of water of all cups. We can easily get the answer for all 3 questions above.

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can maintain an int[ , ] array and use DP: Process(int[,] matrix, int c, int v)
int[,] s - the volume of water that each cup will get.
int c - the total volume of water
int v - the volume of cup
s[x,y] = ((s[x-1,y]>v) ? (s[x-1,y]-v) : 0) / 2 + ((s[x-1,y-1]>v) ? (s[x-1,y-1]-v) : 0) / 2
Logic:
1. Initial s[0,0] = c;
2.

for each row x in s
    for each column y in s
        s[x,y] = ((s[x-1,y]>v) ? (s[x-1,y]-v) : 0) / 2 + ((s[x-1,y-1]>v) ? (s[x-1,y-1]-v) : 0) / 2;

Answer of each question above:
1. Count # of int in s that >=v
2. Count # of s[i,j] such that 0<s[i,j]<v
3. Count max i in s such that s[i,j]>0

Time: O(n^2)
Space: O(n^2)

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can maintain an int[ , ] array and use DP: Process(int[,] matrix, int c, int v)
int[,] s - the volume of water that each cup will get.
int c - the total volume of water
int v - the volume of cup
s[x,y] = ((s[x-1,y]>v) ? (s[x-1,y]-v) : 0) / 2 + ((s[x-1,y-1]>v) ? (s[x-1,y-1]-v) : 0) / 2
Logic:
1. Initial s[0,0] = c;
2.

for each row x in s
    for each column y in s
        s[x,y] = ((s[x-1,y]>v) ? (s[x-1,y]-v) : 0) / 2 + ((s[x-1,y-1]>v) ? (s[x-1,y-1]-v) : 0) / 2;

Answer of each question above:
1. Count # of int in s that >=v
2. Count # of s[i,j] such that 0<s[i,j]<v
3. Count max i in s such that s[i,j]>0

Time: O(n^2)
Space: O(n^2)

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can maintain an int[ , ] array and use DP: Process(int[,] matrix, int c, int v)
int[,] s - the volume of water that each cup will get.
int c - the total volume of water
int v - the volume of cup
s[x,y] = ((s[x-1,y]>v) ? (s[x-1,y]-v) : 0) / 2 + ((s[x-1,y-1]>v) ? (s[x-1,y-1]-v) : 0) / 2
Logic:
1. Initial s[0,0] = c;
2.

for each row x in s
    for each column y in s
        s[x,y] = ((s[x-1,y]>v) ? (s[x-1,y]-v) : 0) / 2 + ((s[x-1,y-1]>v) ? (s[x-1,y-1]-v) : 0) / 2;

Answer of each question above:
1. Count # of int in s that >=v
2. Count # of s[i,j] such that 0<s[i,j]<v
3. Count max i in s such that s[i,j]>0

Time: O(n^2)
Space: O(n^2)

- g233007 February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The filling of the cups with water need to be affected by the water left in the jug for the rest of cups .So after filling the water in cups at each level, we need to recalculate the water left in jug and the %of water to be filled in each cups as we go down

V =capacity of jug
v = capacity of cup
l = level
n = number of cups at that level
final_l = final level to which cups are to be filled for eg 5th level
initially
l = 1, n = 1;
%pernentage = (V - (l*n*v))/(final_l* final_l - l*n)

After filling the water to the cups at each level we can calculate the percentage of water to be filled in the cups at next level so that all the cups are covered, also store the value of % into an array

- jerry February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int V[10][10] = {0};
void fullyfilled(double c, double v, int n)
{
double diff;
int fullyfilledglasses = 0;
for(int x = 0; x < n; x++)
{
if (c <= 0)
{
break;
}
diff = c / (x + 1);

if( diff >= v)
{

diff = v;

}
else
{
diff = c / (x + 1);
}

c -= diff * (x + 1);

for(int y = 0; y <= x; y++)
{

V[x][y] = diff;
}
}

for( x = 0; x < n; x++)
{
for(int y = 0; y <= x; y++)
{

std::cout << V[x][y] << " ";
}
std::cout << endl;
}
}

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

Please find the below code written in C.

Assuming the volume of jug is 100 and we have stack of 10 level.
I have take the parameters as static. this is only concept,

#include <stdio.h>


int main()
{
    int x=10, y=10;
    float arr[x][y];
    float volume=100; int i,j;
    for(i=0;i<x;i++)
    {
        for(j=0;j<y;j++)
        arr[i][j]=0.0;
    }

   for(i=0;i<x;i++)
    {
        for(j=0;j<y;j++)
        {
            printf("%.1f  ",arr[i][j]);
        }
        printf("\n");
    }

    printf("\n \n \n");

    arr[0][0]=volume;

    for(i=0;i<y-1;i++)
    {
        for(j=0;j<=i;j++)
        {
              if(i>x-1 || j>y-1)
                 continue;

              if(arr[j][i]>1.0)
              {
                   arr[j][i+1]+=(arr[j][i]-1.0)/2;
                   arr[j+1][i+1]+=(arr[j][i]-1.0)/2;
                   arr[j][i]=1;
              }
        }
    }

    for(i=0;i<x;i++)
        if(arr[i][y-1]>1)
           arr[i][y-1]=1;


   for(i=0;i<x;i++)
    {
        for(j=0;j<y;j++)
        {
            printf("%.1f  ",arr[j][i]);
        }
        printf("\n\n");
    }

     return 0;
}

- Sanjay Kumar March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

A simple loop should find out all the answers. Question need to be clearified with interviewer here: How is each level filled? For example:
1. the very middle cup will be filled first and then filling two neighboor cups aftwards, or
2. for even level, filling middle two cup first, but for odd level, filling middle one cup first, or
3. other filling patterns

It will affect how to calculated to last level.

const int V = <value of V>;
const int v = <value of v>;

void code()
{
    int level = 0; //level count
    int numberOfCupFilled = 0;
    int left = V;  //leftover int he jar
    int tmpSum; 
    
    while(true)
    {
        //the sum of each level
        tmpSum = level*v;
        
        if(tmpSum > left)  //found the level here
        {
            numberOfCupFilled += left/v;
            float percentage = (float)(left%v)/v;
            cout << "the level not fully filled is " << level;
            cout << "percentage is " << percentage;
            cout << "number of cup filled is " << numberOfCupFilled;
            
            break;
        }
        
        numberOfCupFilled += level;
        left -= tmpSum;
        level++;
    }
}

- Ric February 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cups will be filled in the same manner , if you imagine a scenario ,i.e. a person filling water in the top most cup of the stack (1st level) , which when filled will automatically flow down to next level's cups and so on.

- prashant February 23, 2012 | Flag


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