Directi Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

Let's work backwards. In order to ensure there's precisely one coin left, we have to be the one to start the pile. That is, if a pile has K coins, and one person picks up K-1 of them, they will win since there is only 1 coin left.

So, we know that the winner of the game is the one who starts the last pile, therefore optimal plays says they will both want to start a pile.

How do you start a pile? Ensure there is just one coin left on the previous pile.

Seems to me, like we have an answer. The person who goes first will always win. They simply have to remove all but 1 coin on the first pile. Their opponent can only remove the final coin because that pile is the leftmost non-empty.

The person who goes first can now repeat this process. Eventually, their opponent will have to pick up the last coin, on the last pile.

- jay March 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

I don't think " The person who goes first will always win" is true for the case below:

who will win if N=5 , A[0]=5 , A[1]=4 , A[2]=3 , A[3]=1, A[4]=2 ;

- nikkhil.srivastava March 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

Good point. It seems we can fix that by saying 'take K-1 coins from the current pile if the next pile has 2 or more coins on it, otherwise take all K' - therefore we are still in the situation where the person who goes first wins.

If the first pile has 1 coin, however, I think the person who goes first will lose.

- jay March 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

The person who goes first will always win except for a case when first pile contain only 1 coin.
In that case first one of both who reach to a pile with coins>1 will win.

For Ex. 1 1 1 2 3 1 1 3
in this case if alice plays first then bob will reach to pile[3] first which is >1,hence bob will win.

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

Alice will win always except for one case ie as follows
If there are odd number of continuous piles which contain only 1 coin each and this sequence of continuous piles start from the first pile itself then Bob will win.

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

take N stacks, each having having equal no of elements. then take two arrays alice[ ] and bob[ ]. pop elements from the stack and insert them into alice[ ] and bob [] alternatively. when only one element in the last stack is remaining, check to see which array (alice [ ] or bob[ ]) has more no of elements. the array with the more no of elements wins !!

- Devang Sinha April 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is similar to the game of Nim. en dot wikipedia dot org/wiki/Nim

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

Winner( piles[], n){
totalattempts=0;
for(i=1; i<=n; i++)
totalattempts += piles[i] / i;
if(totalattempts &1)
print Bob winner
else print Alice winner
}

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

i think if we assume that no pile has 1coin then first player will always in.
as explained above the first player will always pick k-1 coing from ith pile if it has k coins.

> presence of 1 coin in a pile makes it interesting such that winning probability is inverted. i.e. if there is 1 pile having 1 coin then , the player playing second will win.

> so every pile having single coin initially inverts the winning possibility.

the solution should be: count the number of piles with 1 coin and and if they are odd second player wins and if they are even first player wins.

- prashant Upadhyay August 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

B always wins the game even if number of piles are odd or even

- Jo January 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Alice can decide if she wants to play first or second ,which way she can win. If she wants to go first in next pile,then in current pile it should go first if no. of coins in current pile is more than 1 picking all except 1 so that Bob picks the left one and she starts first in next pile. If she wants to go second in next pile,which means Bob should go first in next pile so it must go first in this pile and pick all coins so Bob starts in first place in next pile. By doing so upto first pile if going first is what Alice wishes ,then she wins as she starts the game else Bob wins. so it can be solved using Dynamic Programming.See the code:

#define F 1        //first
#define S 2       //second

bool winner(int arr[],int n)
{
    //arr[i] is no. of coins in ith pile
    //n is the no. of piles
    int Alice[n],state,i;   //doing for alice
    Alice[n-1] = ((arr[n-1] > 1)?F:S);
    for(i = n-2; i >= 0; i--)
    {
        if(Alice[i+1] == F)
        state = ((arr[i] > 1)?F:S);
        else     //Alice going second in (i+1)th pile
        state = F;     //pick all here
        Alice[i] = state;
    }
    //in game Alice is starting first
    if(Alice[0] == F)
    return true;
    else
    return false;      //bob wins
}

- Sumit Monga February 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry the space requirement for the above game can be reduced to O(1) since at ith pile we only need to know the strategy for (i+1)th pile whether should go first or second. So,can be done using two variables.

- Sumit Monga February 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Alice wins iff the number of piles with 1 coin is odd. Bob otherwise.
Alice always take A[i]-1 (if A[i] > 1) coins from every pile. If pile i has only one coin then she has to take at least one coin per pile and then Bob will lead the game (he is capable now to take A[j] -1 coin first and leave Alice with just one coin to be picked up. Same Reasoning holds now for Bob.

- knotman September 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

cnt = number of 1s before the first non-one integer.
if cnt is odd : Bob Wins
else : Alice wins

- Aakash January 12, 2017 | 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