The world’s leading publication for data science, AI, and ML professionals.

Google Foobar Challenge: Level 1

An intro to the secretive coding challenge and a breakdown of the problems

Photo by Mitchell Luo on Unsplash
Photo by Mitchell Luo on Unsplash

What is the Foobar Challenge? 🧐

The Foobar Challenge is a Coding Challenge hosted by Google that can be completed in either Python or Java. I completed the challenge using Python. The challenge has its own server with specific, terminal-style commands.

Image by author.
Image by author.

To add some fun, it’s space themed. The evil Commander Lambda has kidnapped space bunnies and you must rescue them. Each problem adds more context to the backstory. The problems are of various difficulty organized into 5 levels. Each question must be solved within a certain time limit. More time is given for the higher levels.

Personally, I found the Level 1, 2, and 3 problems similar to Leetcode problems, both in terms of difficulty and structure. The problems were straightforward and rarely required optimizing code for speed. On the other hand, Levels 4 and 5 were more complicated and layered multiple concepts into one problem.

How to participate in the Foobar Challenge? 🙋 ‍♀️

There are two ways to participate in the challenge:

  1. Google extends an invitation through the browser.
  2. A friend sends you an invitation code

I came across the challenge through the first method. I was Googling "list comprehension" for a different article, and my browser unfolded to reveal the invitation. I never heard of the Foobar challenge before and was a little circumspect. After verifying this was a legitimate challenge, I accepted.

Needless to say, the element of surprise piqued my interest in the challenge. Not to mention, Google reaching out to me with an exclusive invite definitely gave me a confidence boost as a developer. I saw this as a call to adventure and immediately wanted to devote all my attention to it.

For the second method, you need a friend to send you an invitation code. One invitation code is given after completing Level 2. Another is given after completing Level 4.

If you are interested in participating in the challenge, let me know. I have an unused referral code.

Does Google Use the Challenge for Recruiting? 🤑

At one point, Google used the challenge to find new talent. You’ll find many people online who had a Google recruiter reach out one or two days after completing the challenge. However, it appears that Google hasn’t used the challenge for hiring since 2020.

After finishing Level 3, the Foobar server ask if you want to provide your contact info to a Google recruiter. I provided my info, but never heard from them.

Nevertheless, I still believe this challenge is worthwhile. It will expose you to new coding concepts, sharpen your skills, and ultimately, make you a better developer.

Things to Watch Out For and Other Tips 💡

Google provides a constraints.txt file, but it isn’t very detailed. For instance, it states all standard libraries are allowed with a few exceptions which are listed. However, math and numpy aren’t allowed even though those are common libraries. To test which libraries were allowed, I imported the library, returned the answer of one of the test cases, and then verified the solution. If the solution passed the one test case, the library was acceptable.

Also, comment out all print statements before verifying the code. Any print statements will cause the test cases to fail.

The challenge is very particular about input and output types. If the problem statement asks for a specific input or output type, make sure to supply the correct type.

Lastly, I didn’t have any experience with Python 2.7 prior to this challenge. In fact, I tested many of my solutions for the lower level problems in Python 3. This came back to bite me later in the challenge. Specifically, I was not aware that in Python 2.7.13, / operator performs integer division if the inputs are integers. For instance,

However, in Python 3, / performs float division.

Questions & Concepts 📚

Warning – Spoilers Ahead ⛔️

Below, I breakdown the questions and explain my thought process. I also provide solutions. However, I highly recommend you attempt the problem first. The best part of the challenge is the surprise and satisfaction of solving an elusive problem.

I debated not posting solutions and just explaining the underlying concepts. However, part of coding is learning how to troubleshoot and pinpointing where the code fails. Thus, I decided to post solutions so that if you’re stuck, you can see exactly where your logic diverges.

Level 1: Problem 1 ⭐️

I Love Lance & Janice

You’ve caught two of your fellow minions passing coded notes back and forth – while they’re on duty, no less! Worse, you’re pretty sure it’s not job-related – they’re both huge fans of the space soap opera ""Lance & Janice"". You know how much Commander Lambda hates waste, so if you can prove that these minions are wasting her time passing non-job-related notes, it’ll put you that much closer to a promotion.

Fortunately for you, the minions aren’t exactly advanced cryptographers. In their code, every lowercase letter [a..z] is replaced with the corresponding one in [z..a], while every other character (including uppercase letters and punctuation) is left untouched. That is, ‘a’ becomes ‘z’, ‘b’ becomes ‘y’, ‘c’ becomes ‘x’, etc. For instance, the word ""vmxibkgrlm"", when decoded, would become ""encryption"".

Write a function called solution(s) which takes in a string and returns the deciphered string so you can show the commander proof that these minions are talking about ""Lance & Janice"" instead of doing their jobs.

Languages

To provide a Python solution, edit solution.py To provide a Java solution, edit solution.java

Test cases

Inputs: (string) s = "wrw blf hvv ozhg mrtsg’h vkrhlwv?" Output: (string) "did you see last night’s episode?"

Inputs: (string) s = "Yvzs! I xzm’g yvorvev Lzmxv olhg srh qly zg gsv xlolmb!!" Output: (string) "Yeah! I can’t believe Lance lost his job at the colony!!"

Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.

Coincidentally, I recently built an app for a shift cipher and this problem immediately reminded me of that. Read more about the shift cipher here:

Shift Cipher in Streamlit

Only the lowercase characters need to be deciphered. The other characters remain unchanged. First, create a dictionary to store the encoded characters and their deciphered counterparts. Next, use a for loop to iterate through every character in the input string. If the character is lowercase, pull its deciphered value from the the dictionary. If not, add the character to the answer string.

In my opinion, this problem was designed to test the participant’s knowledge of the str and dict data types and their built-in methods.

Conclusion 📌

I plan to continue describing my Foobar journey and detailing how I solved the problems in future articles. Follow me to read more about the challenge. Also, all feedback is welcome. I am always eager to learn new or better ways of doing things. Feel free to leave a comment or reach out to me at [email protected].

Join Medium with my referral link – Katy Hagerty


Related Articles