10 Algorithms To Solve Before your Python Coding Interview
Tags: Python, Data Engineering, Interview, Algorithms, Data Science

10 Algorithms To Solve Before your Python Coding Interview

In this article I present and share the solution for a number of basic algorithms that recurrently appear in FAANG interviews

Photo by Headway on Unsplash

Why Practicing Algorithms Is Key?

If you are relatively new to Python and plan to start interviewing for top companies (among which FAANG) listen to this: you need to start practicing algorithms right now.

Don’t be naive like I was when I first started solving them. Despite I thought that cracking a couple of algorithms every now and then was fun, I never spent too much time to practice and even less time to implement a faster or more efficient solution. Between myself, I was thinking that at the end of the day solving algorithms all day long was a bit too nerdy, it didn’t really have a practical use in the real daily work environment and it would not have brought much to my pocket in the longer term.

“Knowing how to solve algorithms will give you a competitive advantage during the job search process”

Well…I was wrong (at least partially): I still think that spending too much time on algorithms without focusing on other skills is not enough to make you land your dream job, but I understood that since complex problems present themselves in every day work as a programmer, big companies had to find a standardized process to gather insights on the candidate’s problem solving and attention to detail skills. This means that knowing how to solve algorithms will give you a competitive advantage during the job search process as even less famous companies tend to adopt similar evaluation methods.

There Is An Entire World Out There

Pretty soon after I started solving algorithms more consistently, I found out that there are plenty of resources out there to practice, learn the most efficient strategies to solve them and get mentally ready for interviews (HackerRank, LeetCode, CodingBat and GeeksForGeeks are just few examples).

Together with practicing the top interview questions, these websites often group algorithms by company, embed active blogs where people share detailed summaries of their interview experience and sometimes even offer mock interview questions as part of premium plans.

For example, LeetCode let you filter top interview questions by specific companies and by frequency. You can also choose the level of difficulty (Easy, Medium and Hard) you feel comfortable with:

There are hundreds of different algorithmic problems out there, meaning that being able to recognize the common patterns and code an efficient solution in less then 10 mins will require a lot of time and dedication.

“Don’t be disappointed if you really struggle to solve them at first , this is completely normal”

Don’t be disappointed if you really struggle to solve them at first, this is completely normal. Even more experienced Python programmers would find many algorithms challenging to solve in a short time without an adequate training.

Also don’t be disappointed if your interview doesn’t go as you expected and you just started solving algorithms. There are people that prepare for months solving a few problems every day and rehearse them regularly before they are able to nail an interview.

To help you in your training process, below I have selected 10 algorithms (mainly around String Manipulation and Arrays) that I have seen appearing again and again in phone coding interviews. The level of these problems is mainly easy so consider them as good starting point.

Please note that the solution I shared for each problem is just one of the many potential solutions that could be implemented and often a BF (“Brute Force”) one. Therefore feel free to code your own version of the algorithm, trying to find the right balance between runtime and employed memory.

Strings Manipulation

1. Reverse Integer

Output:

-132

543

A warm-up algorithm, that will help you practicing your slicing skills. In effect the only tricky bit is to make sure you are taking into account the case when the integer is negative. I have seen this problem presented in many different ways but it usually is the starting point for more complex requests.

2. Average Words Length

Output:

4.2

4.08

Algorithms that require you to apply some simple calculations using strings are very common, therefore it is important to get familiar with methods like .replace() and .split() that in this case helped me removing the unwanted characters and create a list of words, the length of which can be easily measured and summed.

3. Add Strings

Output:

2200

2200

I find both approaches equally sharp: the first one for its brevity and the intuition of using the eval( ) method to dynamically evaluate string-based inputs and the second one for the smart use of the ord( ) function to re-build the two strings as actual numbers trough the Unicode code points of their characters. If I really had to chose in between the two, I would probably go for the second approach as it looks more complex at first but it often comes handy in solving “Medium” and “Hard” algorithms that require more advanced string manipulation and calculations.

4. First Unique Character

Output:

1

2

1

###

1

2

1

Also in this case, two potential solutions are provided and I guess that, if you are pretty new to algorithms, the first approach looks a bit more familiar as it builds as simple counter starting from an empty dictionary.

However understanding the second approach will help you much more in the longer term and this is because in this algorithm I simply used collection.Counter(s) instead of building a chars counter myself and replaced range(len(s)) with enumerate(s) , a function that can help you identify the index more elegantly.

5. Valid Palindrome

Output:

True

The “Valid Palindrome” problem is a real classic and you will probably find it repeatedly under many different flavors. In this case, the task is to check weather by removing at most one character, the string matches with its reversed counterpart. When s = ‘radkar’ the function returns True as by excluding the ‘k’ we obtain the word ‘radar’ that is a palindrome.

Arrays

6. Monotonic Array

Output:

True

False

True

This is another very frequently asked problem and the solution provided above is pretty elegant as it can be written as a one-liner. An array is monotonic if and only if it is monotone increasing, or monotone decreasing and in order to assess it, the algorithm above takes advantage of the all() function that returns True if all items in an iterable are true, otherwise it returns False . If the iterable object is empty, the all() function also returns True .

7. Move Zeroes

Output:

[1, 3, 12, 0, 0]

[1, 7, 8, 10, 12, 4, 0, 0, 0, 0]

When you work with arrays, the .remove() and .append() methods are precious allies. In this problem I have used them to first remove each zero that belongs to the original array and then append it at the end to the same array.

8. Fill The Blanks

Output:

[1, 1, 2, 3, 3, 3, 5, 5]

I was asked to solve this problem a couple of times in real interviews, both times the solution had to include edge cases (that I omitted here for simplicity). On paper, this an easy algorithm to build but you need to have clear in mind what you want to achieve with the for loop and if statement and be comfortable working with None values.

9. Matched & Mismatched Words

Output:

(['The','We','a','are','by','heavy','hit','in','meet','our',

'pleased','storm','to','was','you'],

['city', 'really'])

The problem is fairly intuitive but the algorithm takes advantage of a few very common set operations like set() , intersection() or & and symmetric_difference()or ^ that are extremely useful to make your solution more elegant. If it is the first time you encounter them, make sure to check this article:

10. Prime Numbers Array

Output:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

I wanted to close this section with another classic problem. A solution can be found pretty easily looping trough range(n) if you are familiar with both the prime numbers definition and the modulus operation.

Conclusion

In this article I shared the solution of 10 Python algorithms that are frequently asked problems in coding interview rounds. If you are preparing an interview with a well-known tech Company this article is a good starting point to get familiar with common algorithmic patterns and then move to more complex questions. Also note that the exercises presented in this post (together with their solutions) are slight reinterpretations of problems available on Leetcode and GeekForGeeks. I am far from being an expert in the field therefore the solutions I presented are just indicative ones.

You may also like: