https://towardsdatascience.com/10-algorithms-to-solve-before-your-python-coding-interview-feb74fb9bc27 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. 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 # Given an integer, return the integer with reversed digits. # Note: The integer could be either positive or negative. print(solution(-231)) print(solution(345)) Output: -132 543 2. Average Words Length # For a given sentence, return the average word length. # Note: Remember to remove punctuation first. sentence1 = "Hi all, my name is Tom...I am originally from Australia." sentence2 = "I need to work very hard to learn more about algorithms in Python!" 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 # Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. # You must not use any built-in BigInteger library or convert the inputs to integer directly. #Notes: #Both num1 and num2 contains only digits 0-9. #Both num1 and num2 does not contain any leading zero. num1 = '364' num2 = '1836' Output: 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 # Given a string, find the first non-repeating character in it and return its index. # If it doesn't exist, return -1. # Note: all the input strings are already lowercase. print(solution('alphabet')) print(solution('barbados')) print(solution('crunchy')) Output: 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 # Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. # The string will only contain lowercase characters a-z. 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 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 Trueif all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function also returns True. # Given an array of integers, determine whether the array is monotonic or not. A = [6, 5, 4, 4] B = [1,1,1,3,3,4,3,2,4,2] C = [1,1,2,3,7] Output: True False True 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. #Given an array nums, write a function to move all zeroes to the end of it while maintaining the relative order of #the non-zero elements. array1 = [0,1,0,3,12] array2 = [1,7,0,0,8,0,10,12,0,4] Output: [1, 3, 12, 0, 0] [1, 7, 8, 10, 12, 4, 0, 0, 0, 0] 8. Fill The Blanks# Given an array containing None values fill in the None values with most recent non None value in the array array1 = [1,None,2,3,None,None,5,None] Output: [1, 1, 2, 3, 3, 3, 5, 5] 9. Matched & Mismatched Words#Given two sentences, return an array that has the words that appear in one sentence and not #the other and an array with the words in common. sentence1 = 'We are really pleased to meet you in our city' sentence2 = 'The city was hit by a really heavy storm' 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. 10. Prime Numbers Array# Given k numbers which are less than n, return the set of prime number among them # Note: The task is to write a program to print all Prime numbers in an Interval. # Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. n = 35 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. |
Home >
