The world’s leading publication for data science, AI, and ML professionals.

A Few Notes on Coding Interview Problems – Array

What I gradually learned from doing a daily Leetcoding.

Photo by Matteo Di Iorio on Unsplash
Photo by Matteo Di Iorio on Unsplash

Play by their rules.

The Coding challenge is a foundation of a technical interview in software engineering, data engineering, or even data science. Almost all job interviews I have seen or experienced in the market require you to pass several coding challenges to prove that you have sufficient problem-solving skills.

Of course, there are many arguments whether the coding challenge is a proper tool to evaluate your professional skill sets. However, it’s still a standard that you need to be familiar with and master it to land your dream job in tech.

One problem I experienced during the coding Interview preparation is that the topics are vast. You have a chance to face any situation in the coding interview, depending on your luck. For instance, the subject could be a basic array traversing to a sophisticated data structure or dynamic programming.

I found that it’s pretty hard to master and understand the problem because, for some specific problems, it’s required you to have prior knowledge in computer science so that you can solve the problem in the way it should be.

For example, you may come up with a naive common-sense solution problem. You thoroughly write and carefully test the code and pass all the test cases. You finally feel that at least I solved the problem without much help from the hints.

But after submitting the solution, they throw you an error with a time limit that exceeds the threshold. After that, everything falls to the ground, and you need to start thinking about a better solution.


Practice makes perfect.

Photo by Sam Moqadam on Unsplash
Photo by Sam Moqadam on Unsplash

Despite the miserable example I mentioned above, Luckily, many platforms out there are made to help you go through this journey. There are several types of platforms related to this stuff.

Firstly, the platform that is used for the company’s recruitment process. They provide a tool to monitor and evaluate the candidate based on the specific coding problem. The company can compare and rank the candidate to select a few worthy candidates to do an on-site interview from thousands of applications. Thus, this is a place where you need to ace the process so that you have a chance to proceed in the next step of the hiring process.

Secondly, the platform provides a community or place for candidates to practice their coding interview skills—the websites such as Leetcode, HackerRank, etc. So you can start evaluating your skill from the most straightforward problem to the hardest one that I don’t think I can solve it within the interview time limit. Nevertheless, it’s still good to know and be familiar with that problem before the actual interview.


What I already did.

Photo by Olav Ahrens Røtne on Unsplash
Photo by Olav Ahrens Røtne on Unsplash

Recently, I have been doing a daily Leetcode. I try to complete five problems (easy level) a day if possible. My strategy is to solve the problem from the easy to hard level and ranking the question sequence based on the acceptance rate. The higher the acceptance rate, the easier it should be.

My Leetcode heatmap - starting from June Screenshot by author
My Leetcode heatmap – starting from June Screenshot by author

I started doing the Leetcode on the Array problem because it has the highest number of problems in the category section. Also, in my opinion, the Array problem doesn’t require that much prior knowledge compared to other topics. When you learn any Programming languages, you will always face the array or list data structure as a primitive data structure.

The number of questions based on problem category Screenshot by author
The number of questions based on problem category Screenshot by author

Regarding all the Array problems from the above figure, It still has various sub-categories within the main category. So, It’s not that easy to get a whole picture of how the array problems are. The more questions you pass, the more experience you get. That’s simple.


Here we are.

Photo by Todd Quackenbush on Unsplash
Photo by Todd Quackenbush on Unsplash

I heard and saw many comments that solving the coding interview only relies on your common sense. What if I can’t solve the problem? Does that mean I don’t have enough common sense?

The quick answer is no. However, I believe that we can practice and develop our common sense.

When you solve problems or learn from other’s solutions. You strengthen your common sense.

It is like you keep more suitable tools in your toolbox, and when you face a new problem, you can pick or combine a proper tool (the concept, technique, thought, method) to solve the current problem.

In my humble opinion, I think it isn’t easy to come up with an innovative solution for the first time if you have never had an experience with it before. However, It will be easier after you have enough tools in your toolbox.

That’s why I am writing this article to help you guys save your time to go over the questions and elegant solutions in Leetcode. I try collecting what I think it helps solve the Array problem. Also, I can use this article as a reference to review the techniques in the future.

Disclaimer: The following tips are based on my opinion. All code snippets come from the discussion session in the Leetcode problem. Therefore, it may not be the best way to solve the problem.


Traverse over the columns

With basic knowledge of array operation, we can go all the way along with the input array. Then, no matter how much dimension it is, we can access the value based on the index.

My beginning method is to traverse over the column by fixing the column number and goes through each row to get the target element in the column. But when I saw this snippet, I am stunned with the zip(*a) part for a while.

It’s a simple and effective way to traverse through the column in python. To give you more explanation, the *a is to extract each row into three lists. After that, you pass three lists to the zip function to map the element in each index together.

For example, with *a , you will get three separate lists[1,2,3] [4,5,6] [7,8,9] . Then you pass into the zip function, which is mapping elements in the same index together. Here the first element (0th index) resulted from zip would be all element in the (0th index) index (1,4,7) .

Want to apply? – Lucky Numbers in a Matrix

Rotate the array

With the similar concept above, now you can rotate the array 90 degrees with a single line of code. Let me explain to you what is going on in the following code snippet.

When facing this problem, I was traverse over the array’s border and shifted the element based on the array’s length. It’s working, but it’s not that much human-readable code. Also, It contains many lines compared to the below code snippet.

You can see the main concept here is zip(*a[::-1]) components. It’s almost the same as with the previous code, except there is a[::-1] part here. The [::-1] piece usually denotes the reverse array operation in python.

Given a = [1,2,3] , you can reverse it with a[::-1] which result [3,2,1] .

Combined two operations

  1. Reverse the 2D array from bottom to top.
  2. Swap the row and column of the 2D array.

That’s all. Suppose It’s not that intuitive. Let me elaborate it step by step.

  1. Firstly, you reverse the 2D array. So, you got [[7,8,9], [4,5,6], [1,2,3]]
  2. Now, you can see that the order of the first column is what we expect after we rotate the matrix 90 degrees. So how do you get that list of elements instead of the current one? – Traverse over the columns.
  3. With zip(*a) you separate the array into [7,8,9] [4,5,6] [1,2,3] and then map each index together. So you get (7,4,1) (8,5,2) (9,6,3) .
  4. Viola, you already get the result you want.

Here is an example of how you combine what you learn before to solve more complicated operations.

Want to apply? – Determine Whether Matrix Can Be Obtained By Rotation

Leverage bitwise operation

For this question, It’s quite straightforward. You can break the problem into steps. Firstly, you can find the frequency of each character in each word. Then, you just need to find the intersection of the common character. So it’s not that hard to solve it step by step.

However, when I found the solution suggested by Lee215. The solution is so elegant that I can’t think of it with my own thought. He used the &= bitwise operation to update the intersection of common characters. It’s like he knows the in-depth detail of the Counter object very well. Also, this method doesn’t work on the default dict object.

Besides, the collections library is a standard one attached with python. So I think it’s good to learn how to use it so as to shorten your time in the coding interview session.

Importantly, you should know how the Counter object is made, in case the interviewer is very strict to the rule and asks you to explain the step behind the scene.

This is a great solution that expands my knowledge boundary. It is a fresh and innovative way to solve the problem. I was grateful to learn something new. Also, I won’t be able to solve the problem this way if I have never seen it before.

Now, I have another way of updating the intersection of Counter . Furthermore, it could be a union of Counter operation with the same concept if you use the bitwise OR operators. There is a chance to adapt the technique to other situations.

Want to apply? – Find Common Characters.

Sort by multiple keys

Sorting is a basic operation you can do it in python with sorted or list.sort() . The difference is one is an in-place operation, but the other is not.

However, I didn’t know it well enough, and sometimes I need to sort it the hard way. For instance, I can loop and compare each element whether it satisfy my condition and put the right element in the right place.

Here I learned something new that I could sort the value with multiple keys. The key could be the value itself or the value based on other objects.

In the following code, I used the frequency of the number (freq[x]) to sort the array first, then if there is an equal pair of numbers. I sort it again in descending order with a -x statement.

It’s short and quite intuitive when you come back to review the code.

Want to apply? – Relative Sort Array, Sort Array by Increasing Frequency


Final thoughts

Photo by Benjamin Davies on Unsplash
Photo by Benjamin Davies on Unsplash

There are many ways to solve one problem. It depends on how much you know and understands the tool or problem you are currently working on.

To develop a better and faster solution, you have to master both the programming language you use and an idea of data structure and algorithm concept. If you know only the idea but cannot transcribe it into code, it’s useless in the coding interview.

Furthermore, suppose you possess both ideas of how to solve the problem and an ability to transcribe it to code, but if you can’t do it under the time limit and pressure, it’s also pointless.

To pass all these obstacles, you need to have enough experience to realize and recall the right way to solve the problem simultaneously. Nothing can help you here except practice over and over again.

You may get a rejection for the first 10th or 100th of the coding interview. Please don’t feel bad and give up early. Keep going, and one day you will get what you dream of.


Pathairush Seeda

if you like this article and would like to see something like this more.


Related Articles