Image
Arslan Ahmad

Unlock the Top 20 Coding Questions To Pass Google Interview

Practice these 20 coding problems to clear your Google interview
Image

Are you a software engineer dreaming of securing a job at Google?—you are not alone.

Google is one of the most sought-after employers in the tech world, known for its innovation, great work culture, and impactful projects.

But, getting through the Google interview process can be quite challenging and often leaves candidates feeling overwhelmed and unsure of where to start.

If you are in the same boat wondering what to focus on and how to prepare, this blog is here to guide you.

We will break down the top 20 coding questions that will help you pass your Google interview, along with insights into the interview format, what Google looks for, and the types of questions you can expect. Let’s jump right in and get you ready for your Google interview!

Section 1: Understanding Google’s Interview Process

Google started in 1998 when two Ph.D. students, Larry Page and Sergey Brin, created a search engine in their dorm room at Stanford University. Their goal was to organize the world's information and make it universally accessible.

They developed a new algorithm called PageRank to rank web pages based on their relevance and popularity.

The search engine quickly gained popularity, and in 1998, they officially launched Google Inc.

Over the years, Google has expanded beyond search, introducing products like Gmail, Google Maps, and the Android operating system.

Today, Google is part of Alphabet Inc., a parent company that oversees various businesses, including Google.

Google Interview Format

The interview process at Google typically includes several stages:

  • Initial Screening: A recruiter will call you to discuss your background, experience, and fit for the role.

  • Technical Phone Interviews: These involve coding challenges and technical questions, usually conducted over the phone or via video call.

  • On-Site or Virtual Interviews: These consist of multiple rounds, including coding problems, system design questions, and behavioral questions. With remote work becoming more common, these are often conducted virtually now.

What Google Looks For

Google seeks candidates who excel in the following areas:

  • Technical Skills: Proficiency in coding, algorithms, and data structures is crucial.

  • Problem-Solving Ability: They want to see how you approach and solve complex problems.

  • System Design: Ability to design scalable and efficient systems is important. Learn the secret hacks to clear your Google system design interview.

  • Cultural Fit: Google values collaboration, innovation, and a growth mindset. They look for candidates who can work well in teams and are eager to learn and grow.

  • Communication Skills: Clear and effective communication is important, especially when explaining your thought process and solutions. Work on these 6 soft skills to clear your technical interviews.

Types of Questions at Google Interview

Google interviews typically include:

  • Coding Problems: These focus on data structures and algorithms. Common topics include arrays, strings, trees, graphs, dynamic programming, and sorting/searching algorithms.

  • System Design: These questions assess your ability to design large-scale systems. You’ll need to explain how you would build and scale systems, considering aspects like performance, scalability, and reliability.

  • Behavioral Questions: These questions focus on your past experiences and how you handle different work situations. They help the interviewer understand how you work in a team and deal with challenges.

Check out the guide to prepare your System Design interview at Google.

Section 2: 20 Coding Problems To Pass Google Interview

Let us go through the 20 coding problems below:

1. Add Binary

Problem Statement: Given two binary strings a and b, return a sum of a and b represented as binary strings.

Example:

  • Input: a = "10110", b = "10101"
  • Expected Output: "101011"
  • Justification: The binary sum of "10110" (22 in decimal) and "10101" (21 in decimal) is "101011" (43 in decimal).

Difficulty Level: Easy

Solution: Add Binary

2. Valid Mountain Array

Problem Statement: Given an integer array arr, return true if arr is a valid mountain array. Otherwise, return false. The array is called a valid mountain array if:

  • arr.length > 2
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Example:

  • Input: arr = [4, 5, 6, 7, 8, 7, 6, 5]
  • Expected Output: true
  • Justification: The elements increase from 4 to 8 and then decrease from 8 back to 5, forming a mountain shape.

Difficulty Level: Easy

Solution: Valid Mountain Array

3. Set Mismatch

Problem Statement: You are given an array nums of size n, originally containing 1 to n integers.

Unfortunately, due to some error, one number got duplicated, replacing another number that now goes missing from the sequence.

Identify the duplicated number and the missing number and return them as a pair.

Example:

  • Input: nums = [1,3,3,4]
  • Expected Output: [3,2]
  • Justification: Here, 3 appears twice, and 2 is missing from the sequence.

Difficulty Level: Easy

Solution: Set Mismatch

4. Is Subsequence

Problem Statement: Given two strings s and t, return true if the string s is a subsequence of the string t. Otherwise, return false.

A subsequence of a string is a new string that can be formed from the original string by eliminating some (can be zero) of the characters without changing the order of the remaining characters.

Example:

  • Input: s = "hello", t = "xyhealoloo"
  • Expected Output: true
  • Justification: By removing "x", "y", "a", "o", and the second "o" from t, s can be obtained while maintaining the original order.

Difficulty Level: Easy

Solution: Is Subsequence

5. Length of Last Word

Problem Statement: Given a string str which contains words and spaces, return the length of the last word in the str.

A word is defined as a substring containing non-space characters only.

Example:

  • Input: str = "Fly to the moon"
  • Expected Output: 4
  • Justification: The last word is "moon" which consists of 4 characters.

Difficulty Level: Easy

Solution: Length of Last Word

6. Monotonic Array

Problem Statement: Given an array nums containing integers, return true if the given array is monotonic. Otherwise, return false.

An array is monotonic if it follows the below conditions.

  • An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j].
  • An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].

Example:

  • Input: nums = [1, 5, 10, 10, 15, 16, 90]
  • Expected Output: true
  • Justification: The array consistently ascends or remains equal, thus it is monotone increasing.

Difficulty Level: Easy

Solution: Monotonic Array

7. Find the Difference

Problem Statement: You are given two strings s and t. The string t is formed by taking the string s, randomly shuffling all its characters, and then adding one additional character at a random place.

Return the extra character that was added to t.

Example:

  • Input: s = "a", t = "aa"
  • Expected Output: “a”
  • Justification: The string t contains an extra "a" compared to the string s.

Difficulty Level: Easy

Solution: Find the Difference

8. Binary Tree Paths

Problem Statement: Given the root node of a binary tree, return all unique paths from the root node to the leaf node in any order.

Example:

  • Input: root = [1,2,3,null,null,4,5]
  • Expected Output: ["1->2", "1->3->4", "1->3->5"]
  • Justification: The binary tree has one root-to-leaf path for the left child (1->2) and two for the right child, branching into 4 and 5 respectively.

Difficulty Level: Easy

Solution: Binary Tree Paths

9. Merge Strings Alternately

Problem Statement: Given two strings, word1 and word2, return the merged string after merging each character of both strings in alternate order, starting with the word1.

If one string is longer than the other, append the remaining characters of the longer string to the end of the merged string.

Example:

  • Input: word1: "dog", word2: "cat"
  • Expected Output: "dcoatg"
  • Justification: The characters from both strings are alternated, starting with the first character of the first string. Since both strings are of equal length, no extra characters remain at the end.

Difficulty Level: Easy

Solution: Merge Strings Alternately

10. 2 Keys Keyboard

Problem Statement: You're given a notepad that initially displays a single character 'A'. You have two actions available to perform on this notepad for each step:

  • Copy All: It allows you to copy everything on the screen.

  • Paste: You can paste the characters which are copied last time. Given an integer n, return the minimum number of operations to print the character 'A' exactly n times on the screen.

Example:

  • Input: n=4
  • Expected Output: 4
  • Justification: Start with 'A', copy it (1 operation), and paste it(1 operations). Now, you have 'AA'. Copy it, and paste it. Total = 4 operations.

Difficulty Level: Medium

Solution: 2 Keys Keyboard

11. Arithmetic Slices

Problem Statement: Given an integer array nums, return the number of arithmetic subarrays of nums.

If any integer array consists of at least three elements and if the difference between any two consecutive elements is the same, it is called arithmetic.

A subarray is a contiguous subsequence of the array.

Example:

  • Input: [1, 3, 5, 7, 9, 10, 11]
  • Expected Output: 7
  • Justification: The segments that form arithmetic sequences are [1, 3, 5], [3, 5, 7], [5, 7, 9], [1, 3, 5, 7], [3, 5, 7, 9], [1, 3, 5, 7, 9], and [9, 10, 11]. Each of these segments has a constant difference of 2 between consecutive elements.

Difficulty Level: Medium

Solution: Arithmetic Slices

12. Letter Combinations of a Phone Number

Problem Statement: Given a string digits containing digits from 2-9, return an array of strings containing all possible letter combinations that the number could represent. You may return the answer in any order.

The mapping of digits to letters (just like on the telephone buttons) is as given below.

  • 1 -> It doesn't map any number.
  • 2 -> "abc"
  • 3 -> "def"
  • 4 -> "ghi"
  • 5 -> "jkl"
  • 6 -> "mno"
  • 7 -> "pqrs"
  • 8 -> "tuv"
  • 9 -> "wxyz"

Example:

  • Input: digits = "47"
  • Expected Output: ["gp", "gq", "gr", "gs", "hp", "hq", "hr", "hs", "ip", "iq", "ir", "is"]
  • Justification: The digit '4' maps to "ghi", and '7' maps to "pqrs". Combining these letters in every possible way gives us the expected output.

Difficulty Level: Medium

Solution: Letter Combinations of a Phone Number

13. 01 Matrix

Problem Statement: Given an m x n matrix mat containing only 0 and 1, return the distance of the nearest 0 for each cell.

The distance between two adjacent cells of mat is 1.

Example:

  • Input: mat = [[0,1,1],[1,1,0], [1,0,1]]
  • Expected Output: [[0,1,1],[1,1,0],[1,0,1]]
  • Justification: Starting with the zeros, we update their neighbors. The zeros remain zeros since they are already at the nearest zero. The ones directly adjacent to a zero become ones, demonstrating their proximity.

Difficulty Level: Medium

Solution: 01 Matrix

14. Minimum Area Rectangle

Problem Statement: Given an array of points in the X-Y plane points where points[i] = [xi, yi], return the minimum area of a rectangle formed from these points. The sides of the rectangle should be parallel to the X and Y axes.

If there is not any such rectangle, return 0.

Example:

  • Input: points = [[1,2],[2,1],[1,0],[0,1]]
  • Expected Output: 0
  • Justification: There are no four points that can form a rectangle with sides parallel to the axes, so the minimum area is 0.

Difficulty Level: Medium

Solution: Minimum Area Rectangle

15. Fair Distribution of Cookies

Problem Statement: You are given an array of integers cookies, where cookies[i] represents the count of cookies in the ith bag, and integer k that represents the number of children.

You must divide these bags among k children such that every bag goes entirely to one child, with no splitting of the contents.

The maximum number of total cookies obtained by a single child in the distribution is called the unfairness of the distribution.

Return the minimum unfairness of all distributions.

Example:

  • Input: cookies = [5,6,7,3,4], k = 2
  • Expected Output: 13
  • Justification: The most balanced way to distribute the cookies is to give bags [5,7] to one child and [6,3,4] to the other, resulting in one child getting 13 cookies and the other getting 12. The maximum number of cookies received by a child is 13, which is the minimal unfairness possible here.

Difficulty Level: Medium

Solution: Fair Distribution of Cookies

16. Find Leaves of Binary Tree

Problem Statement: Given the root of a binary tree, collect a tree's leaf nodes by following the given rules and return them in a 2D array.

  • Collect all the leaf nodes from the left to right.
  • Remove all the leaf nodes.
  • Repeat until the tree is empty.

Example:

  • Input: root = [1,2,3,4]
Image
  • Expected Output: [[4,3],[2],[1]]
  • Justification: Initially, nodes 4 and 3 are leaves and are removed in the first round. In the second round, node 2 becomes a leaf. Node 1 is the last one remaining and is removed in the final round.

Difficulty Level: Medium

Solution: Find Leaves of a Binary Tree

17. Container with Most Water

Problem Statement: Given an array of non-negative integers, where each integer represents the height of a vertical line positioned at index i. You need to find the two lines that, when combined with the x-axis, form a container that can hold the most water.

The goal is to find the maximum amount of water (area) that this container can hold.

Note: The water container's width is the distance between the two lines, and its height is determined by the shorter of the two lines.

Example:

  • Input: [1,3,2,4,5]
  • Expected Output: 9
  • Justification: The lines at index 1 and 4 form the container with the most water. The width is 3 * (4-1), and the height is determined by the shorter line, which is 3. Thus, the area is 3 * 3 = 9.

Difficulty Level: Medium

Solution: Container with Most Water

18. Candy

Problem Statement: You are given ratings array of integers where ratings[i] is rating value assigned to the ith child. You are giving candies to these n children who are standing in single row.

Follow below rules to give candies to each child:

  • Each child must have at least one candy.

  • Children with a higher rating get more candies than their neighbors. Return the minimum count of candies you require to distribute candies to the children.

Example:

  • Input: ratings = [4, 2, 3, 4]
  • Expected Output: 8
  • Justification: The second child gets 1 candy (lowest rating), the third gets 2 candies (higher than the second), the first gets 2 (higher than the second but equal to the third), and the fourth gets 3 (highest rating).

Difficulty Level: Hard

Solution: Candy

19. Burst Balloons

Problem Statement: You are given a row of n balloons, each marked with a specific number i.e. ith ballon is marked with nums[i], where nums is an integer array. You are asked to burst all the balloons.

When you burst the ith balloon, you get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 indexes are out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.

Return the highest possible coin total after popping all the balloons.

Example:

  • Input: [3, 1, 5]
  • Expected Output: 35
  • Justification: Popping balloon 1 (value 1) first earns 315=15 coins, leaving [3, 5]. Next, pop 3 to earn 135=15, and finally pop 5 to earn 151=5, for a total of 15+15+5=35 coins.

Difficulty Level: Hard

Solution: Burst Balloons

20. Trapping Rain Water

Problem Statement: Given an array of positive integers height, where height[i] represents the height of the bar in the elevation map and each bar has width 1, return how much water it can trap after raining.

Example:

  • Input: height = [4, 0, 3, 0, 2]
Image
  • Expected Output: 5
  • Justification: The first and third bars form a container that traps 3 units of water. The third and fifth bars trap an additional 2 units. Therefore, the total trapped water is 5 units.

Difficulty Level: Hard

Solution: Trapping Rain Water

Bottomline

Preparing for a Google coding interview requires dedication and practice.

You can improve your coding skills by practicing regularly and solving a variety of problems similar to the ones covered in this guide to be well-rounded.

Also, conduct mock interviews to get comfortable with the format and enhance your communication skills.

Lastly, after solving problems, review your solutions to learn from any mistakes.

Coding Interview
Coding Interview Questions
Coding Patterns
Google
More From Designgurus
Annual Subscription
Get instant access to all current and upcoming courses for one year.
Recommended Course
Image
Grokking the Coding Interview: Patterns for Coding Questions
Join our Newsletter
Read More
Image
Arslan Ahmad
Mastering the FAANG Interview: The Ultimate Guide for Software Engineers
Image
Arslan Ahmad
8 Reasons Why Everyone Must Practice Programming
Image
Arslan Ahmad
Google System Design Secrets: Insider Tips and Strategies for Acing Your Interview
Image
Arslan Ahmad
12 Weeks Tech Interview Preparation Bootcamp
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.