Google Python Challenge #1

The cake is not a lie.

Keno Leon
Keno Leon
Feb 17, 2020 · 9 min read

The other day I was googling some Python stuff and an invitation to a Google code challenge appeared, (the so challenge ), I am not fond of code challenges or leetcode interviews for a number of reasons, but curiosity got the best out of me and I decided to check it out, here’s a guide through the interesting setup and first challenge.

You get an admittedly cool looking IDE that behaves like your regular terminal, type help and the relevant commands appear, you can open a code editor on the right after requesting a challenge and navigating to the solution file

The whole challenge is wrapped in a scifi-ish story that you might or might not find amusing, regardless it’s an extra layer that you have to go through to understand the code questions, here’s the first one ( we’ll revisit it later ):

You are additionally given 2 test cases and the corresponding solutions:

A big gotcha is that there are other Hidden Test cases that you must pass in order to solve the question, what kind and what they are is up to you to find out or imagine, this along with the confusing problem descriptions is in part what makes this challenge difficult, once you have a potential solution you can verify it and see how it does, your final solution won’t be accepted unless it passes all tests.

One last but critical constraint is Time, you are given a set amount of time and a counter ( a couple of days in this case), if you decide to leave the challenge and close your window, the timer will keep on ticking, so you also need to sign in so you don’t loose your progress, you can find the challenge page again by googling for it or saving a link to it.

Let’s now turn to this specific problem, let’s reiterate (bolded the relevant bits) and restate :

I don’t know about you, but my brain works better when things are presented ( and I do the figuring out ) graphically, since we already have a couple of test cases, we can do just that, here’s the first pattern and how it looks on a cake seen from a top view:

This might not make sense yet, so let’s show the divisions for each letter:

And finally the division for the repeating pattern:

We’ll talk about the edge cases later, but now that we have more or less a good idea of the problem let’s start coding, we need a function that takes some letters, finds a pattern and spews out the number of repeating occurrences of said pattern , this was my initial solution:

This method unfortunately did not pass all the tests so we need to go deeper and create a number of extra edge cases to find the remaining solutions, the fact that the exact pattern is also not being extracted abccba in the second solution is something that also needs to be addressed.

In order to move forward, we need to first find out if the pattern has an odd or even number of characters, this might seems counterintuitive, but it’s a simple way of starting to find edge cases, consider this one :

Given an even number of slices, there should be an even number of divisions with no extra slices, but given an uneven number of slices like in this case, you could have some extra slices, remember that the problem asks for the maximum number of equal parts, 2 in this case. Identifying odd/even should be easy using the modulo operator:

The next item on the list is to find the full pattern, which can be done by finding the index at which the pattern repeats, which in turn can be found doubling the string, finding the index and then extracting the pattern at said index:

Another edge case that needs to be considered is a single character repeating pattern like this one:

This is simply done by measuring the pattern length after the previous step:

So at this point we should have enough to make a final script with all the cases and conditions we’ve gone through, here’s a very verbose take:

There are 6 test cases :

And the corresponding output:

Once refactored into a smaller less verbose script all the tests passed; you then get greeted with a bunny and you can progress further down the challenge.

The eagle eyed might have noticed the incorrect 4th caseabcabcabc where my script states that 2 is the maximum number of slices where 3 is the expected output, I chose to leave this error here because I only discovered it while writing this post 🙃, (you can fix it as an exercise ). It also shows how you can pass these tests and still be wrong, which leads us to our next issue…

You can skin this cat in any number of ways, I can’t remember precisely which Stack Overflow answers I used, but I remember at one point I was overwhelmed with options, and this is another problem with code challenges, your knowledge of the domain (both the language and the problem itself) improves as time goes by and your answer becomes better, ( unless you run out of time like me ). This and many other challenges put a premium on fast coding specific problems which you might or not have been primed for.

I’ll probably make a separate post dealing with this aspect, but as the deadline nears, or even before that your stress levels are bound to rise, and stress affects your memory and comprehension in negative ways

Whats more, if you had put me in front of a terminal without internet I probably would have failed, perhaps because my day to day does not involve strings ( it currently involves Neuroscience research, Python GUIs and Quant stuff so thats fresh in my memory ), in any case, it is both an interesting observation and a roadmap for solving and preparing for these types of challenges (don’t get stressed and practice) :

“I love deadlines. I like the whooshing sound they make as they fly by.” ~Douglas Adams

I still dislike coding challenges, even if they are wrapped in a cool IDE and come dangling a potential Google interview, regardless of my opinion these are the norm and I fully understand that most people want to learn more about them and how to better prepare for them.

This short article hopefully showed you my experience and what to expect if you get an invitation to the Google challenge (keep on googling python stuff to improve your chances of getting one), but perhaps more interestingly showed how to dissect a Python coding challenge (or many other problems) and the obvious yet overlooked situation where you get better the more time you spend on any problem or language as well as the tricky situation where stress plays against you solving new problems.

Thank for reading !


The Startup

Get smarter at building your thing. Join The Startup’s +724K followers.

Keno Leon

Written by

Keno Leon

AI, Software Developer, Designer :

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Keno Leon

Written by

Keno Leon

AI, Software Developer, Designer :

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store