Google looks for software engineers who want to grow and evolve with their fast-paced business. If you're eager to embrace new problems, you can find many opportunities to switch teams, lead projects, and pursue your career goals at Google.
Google coding interviews are crucial for screening candidates who will thrive in the long term. This allows interviewers to evaluate your technical skills and ability to think through complex problems.
The exact setup of your interviews can vary across teams. However, software engineering candidates can expect to write code several times during the interview. You'll need to be ready to solve brand-new coding problems in real time.
So, how can you prepare to tackle coding problems in your Google interview?
Fortunately, you can solve most of Google's coding questions by identifying the underlying patterns. These will help you break down problems into more manageable parts. From there, you can apply strategies and algorithms that help you reach a solution quickly.
To streamline your interview prep, we’ve identified 9 patterns that you're most likely to see in Google coding problems.
We'll review each pattern in depth and share examples of common coding problems that use it. Let's get started!
The Hash Maps pattern is a tool for storing and retrieving key-value pairs. On average, it offers constant-time complexity for insertion, deletion, and lookup operations. Quicker lookup time makes hash maps ideal for tasks like caching, indexing, or frequency counting.
The core operations of hash maps are the following:
1. Insert: A key-value pair is added, with the hash function determining the index for storage. While typically quick (
2. Search: Values are retrieved by applying the hash function to compute the key’s index, typically a quick operation (
3. Remove: Values are removed by deleting the entry at the key’s computed index. Usually quick (
Using hash maps significantly reduces lookup times to an average of
Let’s see how the following example illustrates the application of the Hash Maps pattern to efficiently solve the given coding problem:
For the given stream of message requests and their timestamps as input, you must implement a logger rate limiter system that decides whether the current message request is displayed. The decision depends on whether the same message has already been displayed in the last
Note: Though received at different timestamps, several message requests may carry identical messages.
We need to know if a message already exists and keep track of its time limit. For problems where two associated values need to be checked, we can use a hash map.
We can use all incoming messages as keys and their respective time limits as values. This will help us eliminate duplicates while respecting the time limit of
Here is how we’ll implement our algorithm using hash maps:
Initialize a hash map.
When a request arrives, check if it’s a new request (the message is not among the keys stored in the hash map) or a repeated request (an entry for this message already exists in the hash map). If it’s a new request, accept it and add it to the hash map.
Let’s look at the code for this solution below:
With our understanding of Hash Maps established, let's discuss the next coding pattern.
The Merge Intervals pattern is a powerful coding technique for problems involving meeting times or intervals of some nature. This technique is particularly useful when we need to deal with a set of intervals and perform operations such as merging overlapping intervals or determining their intersections.
In this technique, we typically start by sorting the given intervals based on their start or end times, which helps identify the overlapping intervals efficiently. Once we have this interval information, we can swiftly perform the tasks based on the problem's requirements. The Merge Intervals pattern has many applications in multiple scenarios, including scheduling algorithms, resource allocation problems, and calendar management systems. From analyzing time-based data to consolidating meeting schedules, this coding technique offers an elegant solution for handling interval-related operations effectively.
Let’s see how the following examples illustrate the application of the Merge Intervals pattern to efficiently solve these problems:
We're given a list containing the schedules of multiple employees. Each person's schedule is a list of non-overlapping intervals in sorted order. An interval is specified with the start and end times, both positive integers. Find the list of finite intervals representing the free time for all the employees.
The intuition behind this solution involves merging the individual schedules of all employees into a unified timeline. By doing this, we can identify the common free time intervals where none of the employees are occupied. The key idea is to find the gaps or intervals between the time slots of these merged schedules.
We use the following variables in our solution:
previous: Stores the end time of the previously processed interval.
i: Stores the employee’s index value.
j: Stores the interval’s index of the employee, i.
result: Stores the free time intervals.
The steps of the algorithm are given below:
We store the start time of each employee’s first interval along with its index value and a value
We set previous to the start time of the first interval present in a heap.
Then we iterate a loop until the heap is empty, and in each iteration, we do the following:
Pop an element from the min-heap and set i and j to the second and third values, respectively, from the popped value.
Select the interval from input located at i,j.
If the selected interval’s start time is greater than previous, it means that the time from previous to the selected interval’s start time is free. So, add this interval to the result array.
Now, update the previous as
If the current employee has any other interval, push it into the heap.
After all the iterations, when the heap becomes empty, return the result array.
Let’s look at the code for this solution below:
Now, let's look at another problem that can be solved using the Merge Intervals pattern.
Given an input array of meeting time intervals, intervals, where each interval has a start time and an end time, find the minimum number of meeting rooms required to hold these meetings.
An important thing to note here is that the specified end time for each meeting is exclusive.
The optimized approach to solve this problem is to use the Merge Intervals technique. In this approach, we sort the given meetings by their start time and keep track of the end times of each meeting. We do this by initializing a min-heap and adding the end time of the first meeting to the heap. The heap data structure enables us to efficiently keep track of the meeting with the earliest end time, thereby providing insight into the availability of meeting rooms.
Then, for each subsequent meeting, we check if the room occupied by the earliest ending meeting, the minimum element of the heap, is free at the time of the current meeting. If the meeting with the earliest end time has already ended before the start of the current meeting, then we can use the same meeting room again for the current meeting. We remove the meeting with the earliest end time from the heap and add the end time of the current meeting to the heap. If the earliest ending meeting has not ended by the start of the current meeting, then we know that we have to allocate a new room for the current meeting therefore, we add its end time to the heap.
After processing all the meeting intervals, the size of the heap will be equal to the number of meeting rooms allocated. This will be the minimum number of rooms needed to accommodate all the meetings.
Let’s look at the code for this solution:
Now that we've covered the Merge Intervals, let's move on to another frequently asked coding pattern.
The Knowing What to Track pattern is a strategy for efficiently solving problems by tracking certain properties of the input elements. An example of such a property is the frequency of the occurrence of elements in an array or a string. Tracking such a property can often help derive efficient solutions. This pattern operates in two main phases: the tracking phase and the utilization phase. During the tracking phase, we iterate through the dataset and tally the frequency of each element using appropriate data structures like hash maps or arrays. Once frequencies are calculated, we transition to the utilization phase, where we apply this frequency information to solve specific problems. These problems often involve finding the most frequent element, identifying unique occurrences, or detecting patterns within the dataset. To determine if a problem can be solved using this pattern, look for scenarios where frequency tracking or pattern recognition is essential. The famous interview problems that can be solved with this pattern include Palindrome Permutation, Valid Anagram, Design Tic-Tac-Toe, and Group Anagrams.
Let’s take a closer look at how the following coding problem can be efficiently solved with the Knowing What to Track pattern:
Given an array of integers, arr, and a target, t, identify and return the two indexes of the two elements that add up to the target t. Moreover, the same index can’t be used twice, and there will be only one solution.
Note: We will assume that the array is zero-indexed and the output order doesn’t matter.
We will use a hash map to solve the two-sum problem because it allows us to perform lookups in a constant time, enabling us to quickly check if the difference between the target value and each value of the array already exists in the hash map. If the difference exists in the hash map, we have found the two numbers that add up to the target value, and we can return their indexes. If not, we add the current number and index to the hash map and continue iterating through the input array.
We will first create an empty hash map to store the numbers and their indexes to implement this algorithm. Then, we will iterate over the input array, and for each number in the array, we will calculate its difference (the difference between the target value and the number). Next, we will check if the difference exists in the hash map as a key. If it does, we will retrieve the value of the difference from the hash map, which is the index of the difference value in the array and return it with the current index as the solution to the problem. If the difference is not in the hash map, we will add the current number as a key and its index i as a value to the hash map. We will continue iterating through the input array until we find a pair of numbers adding to the target value. Once we find such a pair, we will return their indexes.
Let’s look at the code for this solution below:
Now that we've discussed Knowing What to Track, let's focus on another important coding pattern.
Custom data structures are essentially modified versions of existing data structures tailored to address specific needs. We often must go beyond standard data structures like arrays and hash tables to tackle unique challenges more effectively. For instance, a web crawler that processes numerous pages and URLs might use a specialized "URL queue" to manage these URLs efficiently, ensuring they are unique and prioritized based on relevance. Custom data structures involve creating custom classes that encapsulate the necessary functionality and properties to efficiently manage and manipulate the data. By designing data structures optimized for the problem domain, we can improve the performance and readability of our code while simplifying complex operations. To determine if a problem can benefit from the Custom Data Structures pattern, consider scenarios where standard data structures like arrays, lists, or maps are not sufficient or where specialized operations need to be performed frequently. Common problems suitable for this pattern include implementing priority queues, disjoint-set data structures, or specialized graph representations.
Let’s see how the following example illustrates the application of the Custom Data Structures pattern to efficiently solve the given coding problem:
Implement an LRU cache class with the following functions:
Init(capacity): Initializes an LRU cache with the capacity size.
Set(key, value): Adds a new key-value pair or updates an existing key with a new value.
Get(key): Returns the value of the key, or −1 if the key does not exist.
If the number of keys has reached the cache capacity, evict the least recently used key and add the new key.
As caches use relatively expensive, faster memory, they are not designed to store large data sets. Whenever the cache becomes full, we must evict some data from it. There are several caching algorithms to implement a cache eviction policy. LRU is a very simple and commonly used algorithm. The core concept of the LRU algorithm is to evict the oldest data from the cache to accommodate more data.
This problem can be solved efficiently if we combine two data structures and use their respective functionalities, as well as the way they interact with each other, to our advantage. A doubly linked list allows us to arrange nodes by the time they were last accessed. However, accessing a value in a linked list is
Here is the algorithm for the LRU cache:
Set:
If the element exists in the hash map, then update its value and move the corresponding linked list node to the head of the linked list.
Otherwise, if the cache is already full, remove the tail element from the doubly linked list. Then delete its hash map entry, add the new element at the head of the linked list, and add the new key-value pair to the hash map.
Get:
If the element exists in the hash map, move the corresponding linked list node to the head of the linked list and return the element value.
Otherwise, return -1.
Note that the doubly linked list keeps track of the most recently accessed elements. The element at the head of the doubly linked list is the most recently accessed element. All newly inserted elements (in Set) go to the head of the list. Similarly, any element accessed (in the Get operation) goes to the head of the list.
Let’s look at the code for this solution below:
Now that we've explored the design and implementation of Custom Data Structures, let's explore the next coding pattern in the list.
The Sliding Window pattern is a useful tool for efficiently solving problems involving sequential data such as arrays or strings, where computations on subsets of data must be repeated. In this technique, a window is defined as a contiguous subset of elements within the data that adjusts its boundaries as it moves through it. Sequential information processing is efficient because the window only focuses on relevant subsets of the data at any given time, avoiding unnecessary computations on the entire dataset. Computations are typically updated in constant time by considering elements entering or exiting the window. By subtracting leaving elements and adding new ones, the computational time remains constant with each movement of the window. Problems like Find Maximum in Sliding Window, Repeated DNA Sequences, and Best Time to Buy and Sell Stock are commonly solved using the Sliding Window pattern.
Let’s see how the following example illustrates the application of the Sliding Window pattern to efficiently solve the given coding problem:
Given a string s and an integer k, find the length of the longest substring in s, where all characters are identical, after replacing, at most, k characters with any other lowercase English character.
We can use the Sliding Window pattern which utilizes two pointers to slide a window over the input string. We initialize the start and the end pointer with
Increment the end pointer until the window becomes invalid.
Increment the start pointer only if the window is invalid to make it valid again.
We keep track of the frequency of characters in the current window using a hash map. We also maintain a variable, lengthOfMaxSubstring, to keep track of the longest substring with the same characters after replacements and mostFreqChar to keep track of the frequency of the most occurring character.
We check whether the new character is in the hash map in each iteration. If it is present in the hash map, we increment its frequency by mostFreqChar to update the frequency of the most occurring character so far using the following expression:
Then, we use the following expression to check if the number of characters in the window other than the most occurring character is greater than k:
If the expression above returns TRUE, the number of replacements required in the current window has exceeded our limit, that is, k. In this case, we decrement the frequency of the character to be dropped out of the window and adjust the window by moving the start pointer by
Then, we update lengthOfMaxSubstring with the current window size if the window size is greater than lengthOfMaxSubstring.
Finally, when the entire input string has been traversed, we return the length of the longest substring such that all the characters in the substring are the same.
Let’s have a look at the code for the algorithm we just discussed.
With our understanding of Sliding Window established, let's move on to discussing the next coding pattern.
In many coding interviews, candidates often encounter problems where binary search comes in handy. It's known for its logarithmic time complexity which makes it super efficient. However, it only works when the input data is already sorted. That's where the Modified Binary Search pattern steps in. It is an advanced adaptation of the traditional binary search algorithm, modified to handle more complex scenarios where elements may not strictly meet the standard sorted criteria. This pattern excels in efficiently locating elements or conditions that are not straightforward to find through linear searching, particularly when dealing with rotated arrays, finding boundaries, or solving the random pick weight problem.
By dividing the search space in half, this method significantly reduces the time complexity to
The adaptability of the Modified Binary Search pattern makes it a powerful tool in software development, enhancing the ability to manage and retrieve data efficiently in scenarios where direct comparisons and typical ordering do not apply. This pattern not only streamlines data retrieval processes but also aids in optimizing performance across various programming tasks.
Let’s see how the following example illustrates the application of the Modified Binary Search pattern to efficiently solve the given coding problem:
We’re given an array of positive integers, weights, where weights[i] is the weight of the weights array. The larger the value of weights[i], the heavier the weight is, and the higher the chances of its index being picked.
Suppose that the array consists of the weights
Index 0:
Index 1:
Index 2:
Note: Since we’re randomly choosing from the options, there is no guarantee that in any specific run of the program, any of the elements will be selected with the exact expected frequency.
We can use the Modified Binary Search pattern to speed up the random index-picking process. It reduces the index searching time to i stores the cumulative sum of weights up to index i. Next, we generate a random number between 1 and the total weight. Finally, we use binary search to find the index corresponding to the randomly generated number in the prefix sum array. This approach ensures that elements with higher weights have a proportionally higher chance of being selected while maintaining randomness.
Here’s how the algorithm works:
The Init() method generates a list of cumulative sums using the given list of weights.
The Pick Index() method returns a randomly selected index while considering the provided weights. It works as follows:
Generates a random number, target, between
Uses binary search to find the index of the first cumulative sum greater than the random value. Initialize the low index to high index to the length of the list of cumulative sums of weights. While the low index is less than or equal to the high index, the algorithm:
Calculates the mid index as low high low)
If the cumulative sum at the mid index is less than or equal to target, the low index is updated to mid + 1.
Otherwise, the high index is updated to mid.
At the end of the binary search, the low pointer will point to the index of the first cumulative sum greater than target. Return this index as the chosen index.
Let’s look at the code for this solution below:
Now that we've discussed Modified Binary Search, let's focus on another important coding pattern.
The Two Pointers technique is one of the must-know coding techniques for effective problem-solving. It involves traversing linear data structures like arrays or linked lists using two pointers moving in a coordinated way. These pointers move in either one direction or opposite directions based on the given problem’s requirements until a condition is met or the input is exhausted. This technique is the go-to solution for problems with sequentially arranged data like arrays, strings, or linked lists or if we need to find some paired elements to satisfy certain constraints or conditions. From verifying a palindrome to detecting cycles in the given data, the Two Pointers technique showcases its efficiency by providing solutions with linear time complexity.
The dynamic movement of these pointers makes the technique both efficient and versatile. Pointers can move independently of each other based on specific criteria, advancing through the same or different data structures. Whether they move in tandem or diverge, their synchronized traversal enables swift problem resolution.
Let’s see how the following example illustrates the application of the Two Pointers pattern to efficiently solve the given coding problem:
Given a sequence of non-negative integers representing the heights of bars in an elevation map, the goal is to determine the amount of rainwater that can be trapped between the bars after rain.
An optimized approach to solving this problem utilizes the Two Pointers technique. Instead of separately processing each element's left and right sides, we simplify it into a single iteration using two pointers, left and right, initially positioned at the elevation map's extremes. The key idea is to maintain two variables, left_max and right_max, which track the maximum heights encountered on the left and right. As the pointers move inwards, they calculate the trapped water for each bar based on the lower of the two maximum heights.
Here's the step-by-step algorithm to find the solution:
Start iterating the heights array using two pointers, left and right. To keep track of maximum heights on the leftmost side and the rightmost side, use two variables left_max and right_max.
If left_max is greater than right_max then it means the maximum height on the left side is greater than the maximum height on the right side.
Hence, we proceed to the right side and calculate the trapped water at the current right position based on right_max. Otherwise, we move on to the left side.
Store the amount of water that can be accumulated by taking a difference between the maximum of the respective sides (left_max or right_max) and the current bar’s height.
Keep iterating and updating the pointers at each step until left becomes greater than right.
Let’s look at the code for this solution below:
Now that we've covered the Two Pointers, let's move on to another frequently asked coding pattern.
The Graphs pattern offers a structured approach for solving problems involving graph data structure, where entities are represented as nodes and their relationships are represented as edges. This pattern involves traversing the graph using various algorithms like Depth-First Search (DFS) or Breadth-First Search (BFS) to explore its vertices and edges systematically.
Breadth-First Search (BFS) starts at a selected node and explores all its neighboring nodes at the current depth level before moving to the nodes at the next depth level. It traverses the graph level by level, often using a queue data structure to keep track of the nodes to be visited. BFS is well-suited for finding the shortest path between two nodes in an unweighted graph.
Depth-First Search (DFS) starts at a selected node and explores as far as possible along each branch before backtracking. It explores one branch fully before moving on to the next branch. DFS is often implemented using recursion or a stack data structure to keep track of the visited nodes and explore the unvisited ones. This algorithm is useful for tasks like finding cycles in a graph, topological sorting, or exploring all possible paths from a starting node.
Common problems suitable for this pattern include finding the shortest path between two nodes, determining the connectivity of a network, or identifying cycles within a graph.
Let’s check out the following interview problem to see how the Graphs pattern works:
Imagine an island with a rectangular shape that touches both the Pacific and Atlantic oceans. The northern and western sides meet the Pacific, while the southern and eastern sides touch the Atlantic. This island is divided into square cells.
To depict the height above sea level of each cell, we use an integer matrix, heights, of size heights[r][c] represents the elevation of the cell at coordinate
When heavy rain pours down on the island every few months, water flows from the island to both the Pacific and Atlantic oceans. The path of flow depends on the heights of the cells.
Consider a cell with a height of
Note: Any cell adjacent to an ocean can channel water into the ocean.
With this information, our task is to return a 2-D array of coordinates. Each entry
The idea stems from the observation that water flows downhill, seeking the lowest possible path. By starting the exploration from the ocean edges and traversing cells equal to or higher in elevation, we can identify regions where water can reach both the Pacific and Atlantic Oceans. The depth-first search (DFS) algorithm is chosen since it naturally emulates the flow of water, recursively exploring cells and marking those that are reachable. The approach focuses on identifying cells that can be reached from both ocean edges, implying that water can flow in both directions, and by finding the intersection of the sets of reachable cells from each ocean, the algorithm effectively pinpoints locations where water can flow to both oceans.
Here’s how the algorithm works:
We initialize the following variables to assist us in performing DFS:
num_rows: This is the total number of rows in the matrix.
num_cols: This is the total number of columns in the matrix.
pacific_reach: This is a hash set that will contain the coordinates of the cells that can be reached from the Pacific Ocean.
atlantic_reach: This is a hash set that will contain the coordinates of the cells that can be reached from the Atlantic Ocean.
The dfs function has the following parameters:
row, col: The coordinates of the initial cell where DFS will begin.
reach: The hash set of the respective ocean for which DFS is called.
Here’s how the dfs function works:
The very first cell passed to the function will always be reachable since this cell is from the border of the matrix about the respective ocean. Therefore, the coordinates of this cell are added to reach.
For the cell above that has been added to reach, we perform DFS by exploring all four directions (top, bottom, left, right) from the cell. This is achieved through the use of a loop that adds the following coordinates to the current (row, col):
(1,0): The cell to the immediate top of the current cell is explored.
(0,1): The cell to the immediate bottom of the current cell is explored.
(-1,0): The cell to the immediate left of the current cell is explored.
(0,-1): The cell to the immediate right of the current cell is explored.
The following conditions are checked for each cell that is explored:
If the cell is out of bounds, it is skipped since it doesn’t exist in the matrix.
If the cell is already in reach, it is skipped since it has already been visited.
If the cell's height is greater than the height of the previous cell, it is skipped since the previous cell can not be reached from this cell.
If the three conditions above are not satisfied, the current cell is a valid cell on which we can continue DFS. So the dfs function is called again for this cell, where it is added to reach, and then its neighbors are explored again.
The dfs function is then called for each cell of the Pacific and Atlantic border in the matrix, which populates pacific_reach and atlantic_reach with the coordinates of the cells that can flow water to the respective oceans.
Finally, we determine the common coordinates in pacific_reach and atlantic_reach and store them in the output array. Because these cells can flow water to both oceans, we return the output array containing them.
Let’s look at the code for this solution below:
With our understanding of Graphs established, let’s explore the last, but certainly not least, coding pattern from Google’s list of frequently asked patterns.
The Dynamic Programming pattern is a technique that helps solve complex problems by breaking them down into simpler subproblems and storing their solutions to avoid redundant computations. This technique is useful when the problem can be divided into overlapping subproblems and optimal substructures. By storing and reusing intermediate results, dynamic programming enables us to solve problems with improved time and space complexity. For instance, a naive recursive approach to check if a string like "rotator" is a palindrome or to calculate Fibonacci numbers can be inefficient due to the repeated calculations of the same subproblems. Dynamic programming addresses these inefficiencies through two main strategies:
Memoization (top-down approach): This technique optimizes recursion by storing the results of subproblems the first time they are computed, preventing redundant calculations.
Tabulation (bottom-up approach): Tabulation constructs a table to store the results of smaller subproblems, gradually building up to solve the larger problem.
Let’s see how the following example illustrates the application of the Dynamic Programming pattern to efficiently solve the given coding problem:
Given a string s, return the longest palindromic substring in s.
If we look at the example above, we notice that any substring of length dp, of size dp[i][j] will store whether the string s[i..j] is a palindromic substring. If the cell dp[i][j] holds the result of the earlier computation, we will utilize it in the
Create a resultant array, res, to store the starting and ending indexes of the longest palindromic substring. Initialize it with
Initialize a lookup table, dp, with FALSE.
Base case 1: The diagonal in the lookup table is populated with TRUE because any cell in the diagonal corresponds to a substring of length
Base case 2: We check whether all two-letter substrings are palindromes and update the res and dp accordingly. We do this by iterating over the string, comparing s[i] and s[i+1], and storing the result at dp[i][i+1]. After that, we also update res if the value of dp[i][i+1] is TRUE, which tells us that the two-letter substring was a palindrome.
After these base cases, we check all substrings of lengths greater than dp[1][1], which will tell whether the remaining string “i”, represented by s[1..1], is a palindrome. We’ll take the logical AND of these two results and store it at dp[0][2] because “zin” is represented by the substring s[0..2]. This way, we’ll avoid redundant computations and check all possible substrings using the lookup table.
Let's implement the algorithm as discussed above:
That's about exploring the coding patterns based on the frequently asked coding questions by Google.
Mastering the patterns we have just discovered to ace your Google interview is important. Understanding the underlying patterns behind the solutions you devise will not only help you tackle similar problems in the future but also demonstrate your depth of understanding to interviewers. We have explored some of the most common coding patterns with the help of interview questions frequently asked by Google, but it’s just a start. Remember, practice makes perfect, so dedicate time to solving problems regularly and seek feedback to improve further. You must focus on the Google behavioral interview as well and may explore the following courses by Educative for even better preparation because they cover a wide range of coding patterns as well as Dynamic Programming patterns, and that too in various programming languages:
Moreover, if you are looking for a customized learning plan, take a look at the following paths by Educative:
With determination, preparation, and a solid grasp of coding patterns, you’ll be well-equipped to tackle any coding challenge that comes your way during the Google interview process. Best of luck!