Road to Google – Part 1: Array and Brute Force

Road to Google, road to life!

Road to Google – an entirely new coding series to challenge you with big tech coding exams. My life-long dream of being hired by Google gives me extra motivation to improve my skill and the journey never ends as long as I breathe here in the plant! Let us begin!!

Last October, I received an unexpected message from the hiring team of AWS (Amazon Web Services) Canada on LinkdIn. They were interested in my skill and reached out to me. That was the most unexpected and exciting message I’ve received last year.

The following day, they sent me a link to an online coding test, and I challenged myself to take the assessment test. That was hard. I mean, extremely hard. Not only was it assessing my coding skill but also my logical thinking and even mathematical skills as well.

Even though I luckily passed the test, I decided not to proceed through the next phases of the interviews. The reason was simple – I was too afraid of the global covid situation, but also I was not really sure about my coding skill.

But the story didn’t end there. The unexpected chance gave me extra motivation to improve my skills and make my dream come true. My dream – working at a big-tech company, especially Google.

If you’ve been following my blog, probably you know my morbid love of Google, my messiah. Android, Chromebook, Gmail, Google Sheets, and the list never ends – I’ve spent the last decade of my tech life almost entirely on the big G’s platforms.

And the opportunity I got out of the blue from AWS gave me the possible future opportunities that I may be able to get a position at Google if my skill meets their criteria. But how? It’s simple – challenging myself every day with coding problems and solving them with my skills.

Here, I present you with an entirely new blogging series – Road to Google!

Throughout the series, I’ll share what I learn from my daily coding-improvement activities with you, and this is quite important for me to journal my learning processes. A good engineer is someone who can explain logically complicated iterations to a child and make them understand with organized and logically coherent descriptions.

And writing what I learned with logically coherent explanations is a crucial part of learning, and I want to share my learning experiences with you.

So, let’s start our first session: array and brute force.

Question:

Given an array of integers, return the indices of the two numbers that add up to a given target.

Explanation:

Okay, so this is one of Google’s coding questions. What it asks you is what pair of numbers in an array can add up to the given target number.

image 01

For example, we’ve got an array that contains [1,3,7,9,2] and the target number 11 as shown in image 01.

image 02

And the pair of numbers that add up to 11 is 9 and 2.

image 03

As you may know, array indexes start from 0. So in this example, elements in the array that satisfy the question are array[3] and array[4] (image 03).

Solution:

Here, let’s write the code to solve the question.

As shown in image 04, we’re using the brute force method to solve the problem.

image 04

For those who don’t know what brute force is, it’s usually come up with a hacker’s hacking technique to log in to a target’s SNS account for example.

Here’s a good explanation I found on Wikipedia.

In cryptography, a brute-force attack consists of an attacker submitting many passwords or passphrases with the hope of eventually guessing correctly. The attacker systematically checks all possible passwords and passphrases until the correct one is found.

And what we did here is basically the same. We’re given the arrays, [1,3,7,9,2], and loop through them until we find the match. In aarray_sample, the program double-loops through the array, and if the double-loop calculation matches with the target sum, it prints out the array’s elements.

Here’s the actual code.

class array_sample {

    public static void findPair(int[] nums, int targetNum){
        // consider each element except the last
        for(int i = 0; i < nums.length - 1; i++) {
            // start from the i'th element until the last element
            for(int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == targetNum) {
                    System.out.printf("Pair found (%d, %d)", nums[i], nums[j]);
                    return;
                }
            }
        }
        // we reach here if the pair is not found
        System.out.println("Pair not found");
    }

    public static void main(String[] args) {
        int[] nums = {1,3,7,9,2};
        int targetNum = 11;
        findPair(nums, targetNum);
    }
}

Afterthoughts:

So, this is what it is. What do you think about it? Do you like it? I think this is quite beneficial for all of us who love coding and give us an extra level of motivation to improve our skills.

Keep coding, keep loving and keep growing!

Via:

Wikipedia

Leave a Reply