Duck Duck Goose
People are standing in a circle playing Duck Duck Goose. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is removed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and wins the game.
Write a function called DuckDuckGoose() that accepts an array of strings and an int k. Start at the beginning and count up to k and remove the person at that position. Keep counting from that index and count up to k over and over until only one person is left. Return a string with the name of the last person left.
k=3
A, B, C, D, E // 1: A; 2: B; 3: C
A, B, D, E // C was removed
B, D, E // A was removed
B, D // E was removed
D // B was removed
// only D is left
k number of times. Once k is hit, skip the enqueue process for that entry, removing it from the queue. Repeat until there is only 1 node in the queue remaining.
O(n * k) time and uses O(n) (linear) space during this process.Familiarize yourself with the grading rubric, so you know how to score the interview.
Look for effective problem solving, efficient use of time, and effective communication with the whiteboard space available.
Every solution might look a little different, but the candidate should be able to test their solution with different inputs to verify correctness.
Assign points for each item on the Rubric, according to how well the candidate executed on that skill.
Add up all the points at the end, and record the total at the bottom of the page.
Record detailed notes on the rubric, to share with the candidate when the interview is complete.
algorithm DUCK_DUCK_GOOSE:
declare array STRINGS <- input array of strings
declare number K <- input number
declare queue QUEUE <- new Queue
for each string in STRINGS:
add string to QUEUE
while QUEUE length is greater than one:
for K number of times:
dequeue string from QUEUE
if this is the kth item removed:
don't enqueue string
else:
re-enqueue string
return last remaining string value in QUEUE