Aqua Education · Follow
Published in · 5 min read · Oct 14, 2023
--
I used to use Java as my algorithm interview language because it is the language I’m most proficient in. I have some experience with Python. I know it has very succinct syntax. I know it’s the most popular general-purpose coding language. However, I never tried to use Python in any of my algorithm interviews. When I get an interview opportunity, I only get very limited time to get prepared and I don’t want to spend time learning a new language and risk my performance. Of course, I want to pick the one that I’m most familiar with. But this year, I decided to use Python and see if it’s better suited for algorithm interviews. So far, I’ve done two interviews using Python this year and I have to say, I loved it. I think you should also switch to Python as your algorithm interviews.
As I mentioned earlier, Python has a really compact syntax. Writing code in Python would increase your coding efficiency in an interview. I switched from Java to Python, so I’m gonna use Java as the benchmark.
- No variable type is needed. Python is a dynamically typed language, while Java is a statically typed language. In Python, there is no need to specify the type of a variable, same applies to function parameters.
- Python has a lot of syntax sugars. Below are Python syntax vs Java syntax to achieve the same goal.
3. Returning multiple values. In Python, you can return multiple values in a method. To get the values returned by that method, you can just do
a, b = function_returning_two_values()
To return multiple values in Java, you might need to create a wrapper class to hold the values.
4. Contain different types in collections. In Python, for example, a list can contain any type. As also stated in the first approach in this leetcode editorial, the Java code is very hacky because of the fact that Java collections can’t store different data types.
5. Python has all the major data structures you need in coding interviews, including stack, queue, priority queue, etc. Javascript also has compact syntax, but one problem with using Javascript for coding interviews is it lacks some of the advanced data structure, like priority queue. Imagine you are given this question in an interview and you picked Javascript as your coding language, will you write your own priority queue class like in this solution?
The average amount of code typed can be reduced by 60%~70% if you switch from Java to Python. That’s a huge time saver, especially for some companies (I’m talking to you Meta), you need to finish two coding questions in one round, so coding fast in very very important.
In short, using Python in coding interviews allows you to focus more on the algorithm itself. While using Java, you get distracted by the language itself, not only the syntax but also the language limitations.
The only inconvenience I’ve seen so far is Python only has minqueue, there is no maxqueue. So if you run into a question that needs maxqueue, you would have to multiply the number with -1 when pushing into the minqueue, and convert it back when popping it out. It’s not too bad.
Now that I have listed the benefits of using Python as the algorithm interview language. You may ask: how to switch to Python? Fortunately, to get to the level of comfortably writing Python code in an interview, it’s quite easy. Here is what I do. I just practiced solving leetcode problems using Python. After solving each problem, I took notes of what Python knowledge I had learned. I use this note as a quick reference for subsequent practice. After practicing for about thirty problems I rarely need to look up the notes or google any more. The thing is in algorithm interviews, you are only going to touch a limited part of the language, mostly the basic syntax and data structures. And because of all the points I listed above, this transition wouldn’t take too much time.
Below is a list of Python knowledge I gained after spending a long time debugging my code
- range(n), n is exclusive
- random.randint(start, end), end is inclusive
- collections initialized with a single item need to wrap the item in a list, for example, if you want to initialize a set with a single string in it, you write
s = set(["abc"]) # this gives you {'abc'}
s2 = set("abc") # this gives you {'b', 'c', 'a'}
- Do not initialize a 2D list with multiplication, because all the nested lists refer to the same list object
x = [[None]*3]*3
>>> x
[[None, None, None], [None, None, None], [None, None, None]]
>>> x[0][0] = 1
>>> x
[[1, None, None], [1, None, None], [1, None, None]]
Instead, do this
[[None]*3 for _ in range(3)]
- a // b does floor division, so 3 // 2 => 1, however -3 // 2 => -2, if you want -3/2 => -1, you need to do int(-3 / 2)
- By looking at the name of the string’s isdigit() method, you would think that you can use it to check if the string is a valid number. However, isdigit() returns False for a negative number string. That’s because isdigit() returns True if all characters in the string are digits and there is at least one character.
- Remember defaultdict.get(key) will return None, not the default value, because get inherits from dict, instead use d[key]
- I mentioned earlier that to check if a collection is not empty, including list, set, and dict, you can simply use if list/set/dict. However, PriorityQueue is different, you need to use if not q.empty()
- Do not abuse variable swap! I’ve found variable swap so easy to use that once I was using it in the wrong way. I was working on the leetcode problem First missing positive, I tried to do something like this
# Basically, I'm trying to swap i with the number at index i in the array.
i, arr[i] = arr[i], i
# Let's take this as an example
arr = [2, 1, 0]
i = 2
i, arr[i] = arr[i], i
By swapping, we want to have is i now becomes 0, and arr is now [2, 1, 2], but what we got is i becomes 0, but arr is still [2, 1, 0]. That’s because when it evaluates the left-hand side, it first does the assignment for i, so now i becomes 0, then it evaluates arr[i], but i is now 0, so instead of assigning value to arr[2], it’s assigning value to arr[0].