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;
}

}``````

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

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]

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

``````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]

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 ??

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.

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

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

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.

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.