Microsoft Interview Question for SDE1s


Team: MS office
Country: United States
Interview Type: In-Person




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

its 11

- jyothi April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

Well, if only 1 person can pass each time, it will take 21 minutes, but 2 people can cross the bridge at the same time, so the minimum optimal time is 10.5 (assume that the bridge has 2 people crossing at any given time, which is impossible if t is not integer), and we can tell that the optimal solution is integer.

11 works, (7 & 10 go, 3 go when 7 reach the other side, at t = 10, 1 go across // one side 3 & 7 & 1, the other 10). Thus, 11 is the optimal solution.

- Mo Lam April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Is it 11 min?

- DashDash April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

3+7=10 & 10 means all will cross in 10 mnts ...
Last one can cross in one mnts hence ... 10+1=11 is optimal...

- Dewendra April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

// We can make this problem more general.
// Consider more than 4 men, for example, a set {15, 14, 10, 7, 5}
// We can find that the mininum time is such a division that two subsets has a mininum difference.
// e.g subset1 = {15,10}, subset2 = {14,7,5};
// Ideally, the min time should be perfect match, pm = SumOfSet/2;
// If there is such a match, we find the answer, otherwise pm++;
// It can be solved recursively.
// C++ code:

#include <iostream>
#include <new>

using namespace std;

bool FindSubsetSumTo( int pm, int array[], int N, bool visited[] );

int MinCrossBridgeTime( int array[], int N );

int main( int argc, char* argv[] )
{
    int a[] = {1, 3, 7, 10};
    int b[] = {15, 14, 10, 7, 5};
    int c[] = {2994, 1443, 533, 2324, 1133, 4444, 2382};

    cout << "minimum time: " << MinCrossBridgeTime( a, 4 ) << endl << endl;
    cout << "minimum time: " << MinCrossBridgeTime( b, 5 ) << endl << endl;
    cout << "minimum time: " << MinCrossBridgeTime( c, 7 ) << endl << endl;

    return 0;
}

int MinCrossBridgeTime( int array[], int N )
{
    int pm = 0;
    for( int i = 0; i < N; i++ ) pm += array[i];
    if( pm %2 == 1 ) pm = pm/2 +1;
    else pm /= 2;
    bool* used = new bool[N];
    bool found = false;
    while( !found )
    {
        for( int i = 0; i < N; i++ ) used[i] = false;
        found = FindSubsetSumTo( pm, array, N, used );
        if( found )
        {
            cout << "subset1: ";
            for( int i = 0; i < N; i++ )
                if( used[i] ) cout << array[i] << " ";
            cout << endl;
            cout << "subset2: ";
            for( int i = 0; i < N; i++ )
                if( !used[i] ) cout << array[i] << " ";
            cout << endl;
            return pm;
        }
        else pm++;
    }
}

bool FindSubsetSumTo( int pm, int array[], int N, bool used[] )
{
    if( pm == 0 ) return true;
    if( pm < 0 ) return false;
    bool found = false;
    int i = 0;
    while( !found && i < N )
    {
        if( !used[i] )
        {
            used[i] = true;
            found = FindSubsetSumTo( pm-array[i], array, N, used );
            if( !found ) used[i] = false;
        }
        i++;
    }
    return found;
}

The result is:

subset1: 1 3 7 
subset2: 10 
minimum time: 11

subset1: 14 7 5 
subset2: 15 10 
minimum time: 26

subset1: 2994 2324 2382 
subset2: 1443 533 1133 4444 
minimum time: 7700

- cctsinghua April 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

min time is the larger of the 2 numbers, not the smaller. You are basically splitting the 2 side almost evenly.

- Mo Lam April 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You're right, just need to change a line. Now it is.

- cctsinghua April 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@DashDash :Min value would be 20 min,here you have to think logic so that 2 max time taking person walks together on the bridge and should not repeat walking to the bridge again back and forth.
I think now you get easily find the solution on your own ;)

- varun April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh this is how I got my answer
7 and 10 will walk together, when 7 reaches other side, 3 will start.
Now 3 and 10 will reach at other side together. Rest 1 is left therefore it will take 11 min. Am i Wrong here?

- DashDash April 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@DashDash: My apology , I thought the question was classic torch question ,in which one person need to come back with the torch , so that others could use that.
Yes , you are right the answer should be 11.

- varun April 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

10 minutes.

Let 10 start in lane 1, and 7 start in lane 2. After 7 minutes, let 3 start in lane 2. After 9 minutes, let 1 start in lane 1.

- Sandeep April 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, at t = 9 minute, everyone except the 7 guy is on the bridge.

--> This is wrong

- Mo Lam April 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Even I think it's 11 mins is optimal

- anil2878 April 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Even I think it's 11 mins is optimal

- anil2878 April 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

11 is Ofcourse an Answer but I think the question is something different.. I have seen somewhat like this is a greater difficulty.

- hprem991 April 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes its 11

- PKT April 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is 11.

- Suresh Meenakshisundaram April 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isnt actually. First set, 7 and 10 leave, with 7 leading the way. The moment 7 reaches (10 still on the bridge), 3 starts. 3 will reach the other end at the same time as 10 reaches. When 3, 7 and 10 finish in 10 minutes, 1 starts. So it will be 10 + 1 = 11 mins

- Anonymous May 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its 11
This is very simple :)

- Sunil B N April 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can some one explain how it is 11 min? 1. 10 & 7 starts, When 7 is finishes 3 starts ..10 and 3 finish at the same time ...1 is still left..he can not walk alone..

- Anaonymous May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Only two people can walk on the bridge means: at max 2.

- Sunil B N May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

11 minutes:
1=10 minutes
2=3+7+1 minutes

- amez June 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer is 17 Mins.

At a given time only 2 people can cross the bridge. SO the optimal way is ,

1,2,7,10 ---------> 1,2 2 mins
1,7,10 <---------2 1 mins
1 --------> 2,7,10 10 mis
1,2 < --------,7,10 2 mins
----------> 1,2,7,10 2 mins

Total = 2+1+10+2+2 = 17mins

- PK June 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first 7 min : from lane1 10 and lane2 :7
next 3 min : from lane1 10(cont.) lane2 3(join)
last 1 min : from lane 1 1

hence 11 is optimal solution

- Aarav July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first 10,1 ->1minute
next 10-1,3 ->3 minute
next 10-1-3,7 -> 7minutes

- Ram October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its 20 Minutes.
1,3,7,10
First : 7,10 l--------l 1,3 = 3 mins
1,7,10 l--------l 3 =3+1=4
2nd 1 l--------l 3,7,10=4+10
1,3 l--------l 7,10 = 14+3
3rd l--------l 1,3,7,10 =17+3

which is 20 minutes . Any doubts

- Anonymous February 26, 2016 | 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