Ace the coding interview in Python: 3 more patterns
Understand these essential patterns and make your interview prep easier!
Hey Grokking Python readers, and happy Thursday!
Welcome to another edition of Grokking Python! In our Dec. 15 edition, we delved into three common interview patterns that are currently popular among interviews.
Today, we will continue exploring important coding patterns for interviews.
Learning coding patterns is crucial for succeeding in programming interviews. Knowing these patterns also demonstrates to interviewers that you have a clear understanding of programming concepts and can apply them to any real-world problems. Other than that, understanding coding patterns can help you write more efficient and maintainable code, which is handy for working on large-scale projects.
With that said, let's dive into three additional key patterns to help you understand and solve common coding interview questions.
Ace Your Interview Prep with Educative
Prepare for the entire engineering interview loop with confidence with hands-on courses developed by FAANG hiring managers.
In-place Reversal of a Linked List pattern
The in-place reversal of a linked list pattern allows us to reverse a linked list without creating a new list or using extra memory, i.e., only using the given nodes.
This pattern is helpful when we have to reverse a set of nodes in a linked list without adding more memory. (To review linked lists and other important data structures, check out this blog.) To achieve an in-place reversal, we need to keep track of the current, next, and previous nodes simultaneously while iterating through the linked list. Using this approach, we can easily change the links between the nodes to point to a different node.
The advantage of using this pattern instead of the naïve approach of iterating with nested loops is that we can achieve a time complexity of O(n) instead of O(n^2) and space complexity of O(1).
Real-world applications
Many problems in the real world use the in-place reversal of a linked list pattern. Let’s look at some examples:
Stocks: Let's say that in the stock market, there are several transactions that need to be executed by multiple brokers. We can assign transactions to each broker in the same order in which they arrived via the in-place reversal pattern.
E-commerce: On an e-commerce website, products are often arranged in a list, with the first half in ascending order based on price and the second half in ascending order based on popularity. To display the products on a landing page, we need to pair them in a way that the first product is cheaper and the second product is more popular. This can be achieved by using the in-place reversal pattern.
Two Heaps pattern
The two heaps pattern employs either two max-heaps, two min-heaps, or one max-heap and one min-heap concurrently to address the problem at hand. For a heap containing n elements, inserting and removing an element both take O(logn) time, while accessing the root element takes O(1) time, which holds the minimum element in a min-heap or the maximum element in a max-heap. (To refresh your knowledge about the heap data structure, check out this blog.)
In some situations, we can divide the data into two parts, enabling the use of a min-heap on one section to locate the smallest element and a max-heap on the other section to find the largest element or vice versa. For instance, two max-heaps could be used to locate the two largest numbers from two different datasets. Conversely, two min-heaps would be utilized when we need to identify the two smallest numbers from two distinct datasets.
Real-world applications
Let's look at some examples of real-world problems that share the two heaps pattern.
Video streaming: It's common for packet drops and buffering to occur during a user session, affecting the user experience. Since we're interested in recording the median number of buffering events that occur in a specific session, we can use the two-heaps pattern in this scenario.
Netflix: Another scenario is when, as part of demographic research, we want to know about the median age of our viewers. Here, we can implement a feature to efficiently update the median age whenever a new user signs up for video streaming via the two heaps pattern.
The Hash Map pattern
The hash map pattern is a method for storing data that aims to reduce the time taken to find and access values.
We store data in hash maps as key-value pairs. It is similar to arrays in that array values are stored against numeric keys, which are referred to as indexes. The value of these indexes is always sequential integers starting from 0, and cannot be selected by the user. As a result, if we need to locate an element within an array and do not know its index, we must search the entire array, which in the worst-case scenario can take O(N) time.
Conversely, hash maps enable the use of flexible keys. Each key is unique and corresponds to a value, allowing us to locate its value in O(1) time.
Real-world applications
Many problems in the real world share the hash maps pattern, such as:
Telecommunications: Creating a phone book that employs a person's name as the key and their phone number as the corresponding value.
E-commerce: Retrieving information about a product by using its product ID as the key.
File system: When a user interacts with a file system, they see the file name and the path. The system uses a hash map to store the relationship between the file name and its path.
Prepare for the entire interview loop with confidence
We’ve now covered six common coding interview patterns over two editions of Grokking Python. If you’ve been following along, you're off to a great start in preparing for coding interviews! However, that alone doesn’t necessarily equate to success in the broader tech interview process. That’s because the coding interview is just one part of the typical interview loop.
To differentiate yourself from other candidates in the loop, you'll need a complete and comprehensive interview prep plan. If you're ready to take charge of your future and launch your plan today, Educative is here to guide you step-by-step with a full range of practical courses designed by FAANG hiring managers. Our materials cover coding interviews, System Design interviews, behavioral interviews, and other related topics, so you can prepare for every phase of the interview.
As always, happy learning!