A photo of some notes I took during foobar challenges.

Google foobar, part i: filtering duplicates

written on February 3, 2023

categories: challenges

tags: google-foobar, Python

One day, while googling around for a solution to a programming problem, I suddenly got invited to the foobar challenge. I had no idea what it was or how it worked but it looked intriguing enough so I thought I'd give it a go.

It's a mysterious thing that appears suddenly in your browser under certain conditions, so of course there's a lot of "lore" around it, what it's for, its difficulty etc. Personally, I got in it for the challenge, and I just wanted to document how I solved some of the problems on foobar.

I'm gonna write a series of posts about these, stating the problem itself first, and then how I proceeded to solve it. However, the problem statement itself is a spoiler for these, since they're timed: knowing the problem in advance and having lots of time to think about it obviously isn't the same. To avoid any spoilers, I'll put one of these warnings at the beginning of each post:

WARNING: Obviously, MAJOR spoilers ahead, for the problems themselves and possible solutions or hints. Even if you're attempting one of these and feel completely stuck, give it another go before reading the rest. Keep reading only if you don't mind getting spoiled!

Problem statement

Inside foobar, there's a whole story about space commanders, bunny minions etc, that I won't share/spoil here. The first problem, in its essence, is very simple:

Given an array of integers and a number n, remove any integers that occur more than n times from the array without modifying the order of the other integers.

Solution

As you can imagine, the solution is pretty simple too. I'm solving all of these in Python:

solution.py

1
2
def solution(arr, n):
    return [element for element in arr if arr.count(element) <= n]

Gotta love Python's simplicity! Let's give it a couple of test runs:

$ python2
Python 2.7.18 (default, Jul  1 2022, 10:30:50)
[GCC 11.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import solution
>>> solution.solution([1, 2, 3], 1)
[1, 2, 3]
>>> solution.solution([1, 2, 3], 0)
[]
>>> solution.solution([1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5], 2)
[1, 2, 2, 5, 5]

Very often in these challenges, the limiting factor is the size of the input; I don't remember exactly what the maximum size of the array was in this one, but I think it was pretty small (i.e. not big enough to really make a difference on how you filter the array). Finally, a thing to note is that they're using a Python 2.7(.something) interpreter to run the code, so sadly there's a bunch of Python 3 stuff we can't use.

And... that's it, really. I'm sure this problem is there just to get you used to the interface, testing/submitting solutions etc. The next part will dissect a more interesting challenge, I promise.


< Moving files between Git repos while preserving history Google foobar, part ii: stashing bunnies >