Amazon Interview Question
Java DevelopersCountry: United States
Assume we have given 5 black cards and 3 red cards. Say K is 4.
So total we have 8 cards.
Create circular linked list of 8 Black cards.
Keep pointer to head node.
Create a variable redCardCount = 3;(Number of red cards) .
Move pointer by K position and replace that card with red card if its black. Mark that node deleted(Just mark it. Don't delete that node). Decrease redCardCount by 1. While moving pointer by K position, make sure we are skipping nodes which are marked deleted.
Do it until redCardCount become 0.
Circular linked list contains position of black and red cards.
C++, Brute Force
0, 6, 3 => BBBBBB
1, 5, 3 => BBRBBB
2, 4, 3 => BBRBBR
3, 3, 3 => BBRRBR
- kyduke March 05, 2018