Groupon Interview Question for SDE1s


Team: APi Team
Country: India
Interview Type: Phone Interview




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

We have to make 2 for loops
1. for placing red cards
2. find position for red card

We are maintaining an array.
I am placing value "1" where we will place red card.

In 2nd inner loop we have to check for 2 conditions:
1. if Index > array bound .. set it as 0.
2. If we get "1" at any location discard it while counting "K"

Here is the code for it.
SetCards(7, 8, 5)

internal void SetCards(int blackCards, int redcards, int K)
        {
            int[] array = new int[blackCards + redcards];

            int index = -1;
            for (int i = 0; i < redcards; i++)
            {
                for (int j = 0; j < K;)
                {
                    index++;
                    if(index == array.GetLength(0))
                    {
                        index = 0;
                    }

                    if (array[index] != 1)
                    {
                        j++;
                    }
                }
                array[index] = 1;
            }


        }

- Mohit March 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So I tried this but the result isn't as expected

func setCards(blackCards:Int, redCards:Int, k:Int) -> [Int] {

var allCardsArray = [Int](count: blackCards + redCards, repeatedValue: 0)

var index = -1
for var i = 0; i < redCards; i++ {

for var j = 0; j < k; {

index++
if index == allCardsArray.count - 1 {

index = 0
}

if allCardsArray[index] != 1 {

j++
}
}
allCardsArray[index] = 1
}

return allCardsArray
}

setCards(5, redCards: 5, k: 2)

Result:
[1, 1, 0, 1, 0, 1, 0, 1, 0, 0]

- Anonymous February 12, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

func setCards(blackCards:Int, redCards:Int, k:Int) -> [Int] {
    
    var allCardsArray = [Int](count: blackCards + redCards, repeatedValue: 0)
    
    var index = -1
    for var i = 0; i < redCards; i++ {
        
        for var j = 0; j < k; {
            
            index++
            if index == allCardsArray.count - 1 {
                
                index = 0
            }
            
            if allCardsArray[index] != 1 {
                
                j++
            }
        }
        allCardsArray[index] = 1
    }
    
    return allCardsArray
}

setCards(5, redCards: 5, k: 2)

Result
[1, 1, 0, 1, 0, 1, 0, 1, 0, 0]

- Anonymous February 12, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

do we need to find all positions where R cards can be placed ??

- braj March 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please correct me if I am wrong.
1. Provided you know the length of the string, create a circular linked list and place the R at K distance to allow the Rs will be eliminated before the B.
2. Iterate and delete the node at K interval.

- wolfengineer March 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think that's almost right, but in part 1 you have to not count any positions marked as red when counting K.

- sje397 March 26, 2014 | 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