As I mentioned in my previous post, I was invited to complete Google's secretive foobar challenge, a very cloak and dagger recruiting tool which prompts prospective candidates based on their Google search queries. Here's a Medium post which goes into more detail of the process. I wanted to revisit these coding challenges and explain my thought process for each one.
Minion Labor Shifts
Commander Lambda's minions are upset! They're given the worst jobs on the whole space station, and some of them are starting to complain that even those worst jobs are being allocated unfairly. If you can fix this problem, it'll prove your chops to Commander Lambda so you can get promoted!
Minions' tasks are assigned by putting their ID numbers into a list, one time for each day they'll work that task. As shifts are planned well in advance, the lists for each task will contain up to 99 integers. When a minion is scheduled for the same task too many times, they'll complain about it until they're taken off the task completely. Some tasks are worse than others, so the number of scheduled assignments before a minion will refuse to do a task varies depending on the task. You figure you can speed things up by automating the removal of the minions who have been assigned a task too many times before they even get a chance to start complaining.
Write a function called answer(data, n) that takes in a list of less than 100 integers and a number n, and returns that same list but with all of the numbers that occur more than n times removed entirely. The returned list should retain the same ordering as the original list - you don't want to mix up those carefully-planned shift rotations! For instance, if data was [5, 10, 15, 10, 7] and n was 1, answer(data, n) would return the list [5, 15, 7] because 10 occurs twice, and thus was removed from the list entirely.
Test cases
Inputs: (int list) data = [1, 2, 3] (int) n = 0 Output: (int list) []
Inputs: (int list) data = [1, 2, 2, 3, 3, 3, 4, 5, 5] (int) n = 1 Output: (int list) [1, 4]
Inputs: (int list) data = [1, 2, 3] (int) n = 6 Output: (int list) [1, 2, 3]
def answer(data, n):
return list(filter(lambda x: data.count(x) <= n, data))
def test_case_1():
assert answer([1, 2, 3], 0) == []
def test_case_2():
assert answer([1, 2, 2, 3, 3, 3, 4, 5, 5], 1) == [1, 4]
def test_case_3():
assert answer([1, 2, 3], 6) == [1, 2, 3]
I'm a fan of functional programming paradigms. Julien Danjou has an excellent post discussing Functional Programming with Python. So by applying a filter to the input list, we can count the occurrences and return those that are less than or equal to n. I felt this was a fairly simple challenge, but I wasn't sure how quickly the difficulty would ramp up.