Amazon Interview Question for Java Developers


Country: United States




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

C++, Brute Force

0, 6, 3 => BBBBBB
1, 5, 3 => BBRBBB
2, 4, 3 => BBRBBR
3, 3, 3 => BBRRBR

void arrangeCard(int r, int b, int k) {
	string ans;
	set<int> s;
	set<int>::iterator it;
	int i, c, n;
	
	ans.assign(r + b, 'B');
	
	for (i = 0; i < r + b; i++) s.insert(i);
	
	c = 0;
	while (r) {
		c = (c + k - 1) % s.size();
		it = s.begin();
		n = 0;
		while (it != s.end() && n < c) {
			it++;
			n++;
		}
		ans[*it] = 'R';
		s.erase(it);
		r--;
	}
	
	cout << ans << "\n";
}

- kyduke March 05, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- krishna May 13, 2018 | 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