You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this post I will share my experience solving the first `Google FooBar Challenge`.
13
+
14
+
While doing some search on Google, I received an invitation to participate in a code challenge by Google. I had no previous idea about it, frankly never heard of it. I accepted the invitation and I was redirected to [FooBar](https://foobar.withgoogle.com). After log in, a `CLI` with the instructions about the challenge and how to request a challenge was provided. The challenge had to be solved within 2 days.
13
15
14
-
I received a surprise code challenge invitation from Google on July 26, 2020 while doing some search on Google. I had no previous idea about it, frankly never heard of it. I accepted the invitation and I was redirected to [FooBar](https://foobar.withgoogle.com). After log in, a `CLI Interface` with the instructions about the challenge and how to request a challenge was provided. The challenge had to be solved within 2 days.
15
-
16
-
### The Challenge
16
+
### The Challenge
17
17
Decrypt a code where 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".
18
18
19
-
__Constraints__
20
-
- Java Runtime Environment(JRE) 8.
21
-
- Limited execution time.
22
-
- Prohibit usage of third-party libraries.
23
-
- Parallel procession not allowed.
24
-
- Character limit to around 3000 (I don’t remember exactly).
19
+
__Constraints for the solution:__
20
+
- Java 8
21
+
- Limited execution time
22
+
- Prohibit usage of third-party libraries
23
+
- Parallel processing disabled
24
+
- Character limit to around 3000 (I don’t remember exactly)
25
25
26
26
### Initial Thoughts
27
27
The rough idea I could come up after the analysis was:
28
28
- Manipulation of the ASCII value of the letters
29
29
- Simple maths, addition/subtraction, on numeric value of letters
30
30
31
-
Since converting this idea to a concrete algorithm took time, I moved to another solution.
31
+
Since converting this idea into a concrete algorithm was taking longer than expected, I moved to another solution.
32
32
33
33
### First Attempt
34
34
The next simplest design for the solution I could come up with consisted of the following data structures:
35
-
-`HashMap` to store the entire `a` to `z` encryption for easy access of decrypted letters.
36
-
-`StringBuilder` to store the deciphered text.
37
-
- Use `Java 8` new method `String.chars()` API which returns a stream of `int` (IntStream) that represents the character codes.
35
+
-`HashMap` to store the encrypted letters and easy access of decrypted letters
At this point I got getting a little bit frustrated because there were no proper errors thrown. The output was just a list of failed tests.
82
-
I lost interest after around an hour and I went back to my previous preparation method, solving challenges from other sources.
83
+
84
+
But again the solution did not pass! At this point I was getting a little bit frustrated because there were no proper errors thrown. The output was just a list of failed tests. I lost interest after around an hour and I went back to my previous preparation method, solving challenges from other sources.
83
85
84
86
### Third Iteration
85
-
The next day I went back to the challenge with around 3 hours remaining. To make sure the time spent here would be worthwhile, I did some search to know more about the challenge. Only then I realized that the challenge was `invitation only`, and was used by Google for recruitment. Those were good enough reasons for me to continue, but then I was seriously running out of time.
87
+
The next day I went back to the challenge with around 3 hours remaining. To make sure the time spent here would be worthwhile, I did some search to know more about the challenge. Only then I realized that the challenge was invitation only, and was used by Google for recruitment. Those were good enough reasons for me to continue, but then I was seriously running out of time.
86
88
87
89
Then I searched for similar problems and solutions to find answers to these questions:
88
-
- How to encrypt a letter by another?
89
-
- What are the standard encryption algorithms?
90
+
- How to encrypt a letter by another
91
+
- What are the standard encryption algorithms
92
+
93
+
A simple example by [Baeldung](https://www.baeldung.com/java-caesar-cipher) answered my queries.
90
94
91
-
A simple example by [Baeldung](https://www.baeldung.com/java-caesar-cipher) answered my queries.
92
95
```java
93
96
StringBuilder result =newStringBuilder();
94
97
for (char character : message.toCharArray()) {
95
-
if (character !='') {
96
-
int originalAlphabetPosition = character -'a';
97
-
int newAlphabetPosition = (originalAlphabetPosition + offset) %26;
`“Any fool can know. The point is to understand.” - Albert Einstein`
109
-
110
-
The standard encryption algorithm used was `Caesar Cipher`. Knowing that the encryption was `Caesar Cipher`, I wanted to understand more which led to [Cryptography](https://github.com/codeanit/til/issues/43), [Cipher](https://github.com/codeanit/til/issues/107) and [more](https://github.com/codeanit/til/issues). Keeping limited time to mind, I skimmed the contents.
108
+
```
109
+
110
+
**“Any fool can know. The point is to understand.” - Albert Einstein**
111
+
112
+
Knowing that standard encryption algorithm used was `Caesar Cipher`, I wanted to understand more that led to [Cryptography](https://github.com/codeanit/til/issues/43), [Cipher](https://github.com/codeanit/til/issues/107) and [more](https://github.com/codeanit/til/issues).
113
+
114
+
Keeping in mind that the time was limited, I skimmed the contents.
111
115
112
116
`A **Cipher** is a method for encrypting a message, intending to make it less readable. **Caesar Cipher** is a substitution cipher that transforms a message by shifting its letters by a given offset.`
113
117
114
118
Baeldung's post also made me realize that understanding of the encryption process was important to decipher. Therefore, based on Baeldung's framework, I created an encryption system that generated the cipher from the challenge.
The next day I reviewed the solution and recalled the overall process. Phew! It was a close call.
177
186
178
-
I wanted to refactor the submitted code as had I submitted in a hurry. Before refactoring, I wrote `Unit Tests` to make sure nothing is broken after the changes are made. These tests did not need to be extensive covering all edge cases; neither should have many use cases covered as it had already passed the test cases of Google.
187
+
I wanted to refactor the submitted code as it was done in a hurry. And prior to refactoring, I wrote `Unit Tests` to make sure that all the methods will work as before. Those tests did not needed to be extensive covering all edge cases; neither should have many use cases, the solution had already passed Google's test cases.
179
188
180
-
Google's first code challenge was fun, I turned it to an exhilarating moment :joy:.
189
+
Previously, I had an interview with Google in July 2019. It was more of a casual talk I had with a nice lady to see if I fit the available role at a certain location. Unfortunately, it did not work out.
181
190
182
-
Previously, I had an interview with Google in July 2019. It was more of a casual talk I had with a nice lady who wanted to know a bit of me and to see if I fit the available role at a certain location. Unfortunately, it did not work out.
183
-
184
-
I am grateful for both opportunities but I must say I have enjoyed this experience more may be because I'm more of a code person.
191
+
I am grateful for both opportunities but I have enjoyed this experience more may be because I'm more of a code person.
185
192
186
193
__Thank you Google!__
187
194
188
-
189
195
If code is a better language to you then please find more at [Github](https://github.com/JavaCheatsheet/codechallenge).
0 commit comments