Join the Shiny Community every month at Shiny Gatherings

Top 7 Data Science Coding Interview Questions and Answers for 2022


If you’re into data science, you know it’s mostly based around SQL, Python, and R. Even though you don’t use these languages in the same way as let’s say, backend developers, data science coding interview questions still put a lot of emphasis on computer science fundamentals, such as data structures and algorithms.

It can be daunting for beginners, as there are already a ton of data science concepts to learn. Adding insult to injury, you now also have to worry about computer science.

Algorithmic questions probably won’t pop up daily on your data science job (if ever), but knowing how to think like a computer scientist elevates your ability to break a single complex problem into a list of smaller, more manageable ones.

Data Science Coding Interview Questions and Answers

Today we’ll share with you the top 7 data science coding interview questions, and we’ll answer them in Python. We’ve chosen Python because that’s the language most data science interviews will be conducted in, and it takes less time and space to write than Java or C++. We won’t go over specific database or data science questions, as that’s a topic for another time.

Python Dash vs R Shiny – Learn which is better for your use case.

Table of contents:


Question #1 – FizzBuzz

This one is a classic and good practice for beginners.

Problem description: Given an integer n, return a string array result where:

  • result[i] == “FizzBuzz” if i is divisible by 3 and 5.
  • result[i] == “Fizz” if i is divisible by 3.
  • result[i] == “Buzz” if i is divisible by 5.
  • result[i] == i in any other case.

You should check for the FizzBuzz condition first, as it checks for multiple conditions. For example, the number 15 is divisible with both 3 and 5, so FizzBuzz should get printed. 15 is also divisible by 3 and 5 individually, and we don’t want Fizz or Buzz printed alone.

Here’s a solution:

And here’s the output for our three test cases:

Image 1 - FizzBuzz test cases output

Image 1 – FizzBuzz test cases output

As you can see, solving the FizzBuzz problem is a no-brainer. You should do additional checks to cover the edge cases – for example, if the value provided to the function isn’t an integer, or if it’s zero or negative. You could capture the edge cases with Python’s assert statement.

Are coding interview questions giving you anxiety? Take a break and learn how to develop an R Shiny Dashboard in 10 minutes.

Question #2 – Reverse an Integer

Yet another simple programming interview question with a couple of interesting edge cases.

Problem description: Given a signed (positive or negative) integer x, return x with its digits reversed. The input integer x must be in range [-2^31, 2^31 - 1] – return 0 if it isn’t.

Seems like a simple question, but we have to think about the edge cases. The input number must be in a given range, which is obvious. But what about integers that end with a zero? For example, reversing the number 1200 should result in 21, not 0021.

Here’s a solution:

And here’s the output from the test cases:

Image 2 - Integer reversal test cases output

Image 2 – Integer reversal test cases output

All of our test cases look good, so we can assume the function works as advertised. You could reverse the number using Python’s slicing notation ([::-1]), but that’s usually not what the interviewer is looking for.

But what happens when you provide an integer outside the range? An assertion error gets raised:

Image 3 - Assertion error on integer reversal

Image 3 – Assertion error on integer reversal

You could further improve the function by adding a check for the data type – but we’ll leave it as homework practice.

Data science is more than extracting knowledge. Learn how to share your data insights using designs people will love.

Question #3 – Find the First Unique Character in a String

It’s a common question that will assess your basic programming skills.

Problem description: Given a string s, find the first non-repeating character and return its index. If it doesn’t exist, return -1.

Seems easy, so let’s give it a try. Remember to lowercase the input string, as letters A and a are identical:

Here’s the result from our test cases:

Image 4 - First unique character indices

Image 4 – First unique character indices

The first test string was “Appsilon”, and the first letter A occurs only once. In the string “Appsilon Poland”, both A and p are repeated, and the first unique letter is s. As for the last string, no character occurs only once, so -1 is returned.

Question #4 – Defanging an IP Address

Your localhost IP address is 127.0.0.1. The process of defanging basically surrounds all the dots with brackets. The end result is 127[.]0[.]0[.]1. Easy!

Problem description: Given a valid (IPv4) IP address, return its defanged version. A defanged IP address replaces every period . with [.].

There aren’t many edge cases here, assuming the string is passed to the function, and that string is an actual IP address. Here’s a solution:

Here’s the test case output:

Image 5 - Defanged IP address test results

Image 5 – Defanged IP address test results

The alternative way would be to use Python’s replace() function to replace every dot with our custom sign, but that’s something you can do on your own.

Question #5 – Check if a String is an Anagram

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the letters exactly once. For example, the words “anagram” and “nagaram” are anagrams.

Problem description: Given two strings a and b, return True if b is an anagram of a, and False otherwise.

Two strings can’t be anagrams if they aren’t of the same length, so you’ll have to check for that first. If the length check passes, you can then store individual letters and their counts in two Python dictionaries – one for each word. If the dictionaries are identical, the strings are anagrams:

Here are the results:

Image 6 - Anagram check test results

Image 6 – Anagram check test results

Our logic works! Sure, there are more Pythonic ways to check for an anagram, such as using the sorted() or set() functions. However, the solution we’ve implemented should work in any programming language, as syntax isn’t Python-specific. That’s exactly what most interviewers will look for.

Question #6 – Check if String is a Palindrome

A string or phrase is a palindrome if after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters it reads the same forward and backward. For example, the string “Bob” is a palindrome, as it’s the same no matter from which side you read it.

Problem description: Given a string s, return True if it’s a palindrome, or False otherwise.

To start, you should lowercase the string and remove all non-alphanumeric characters. Then, you can iterate over every letter of the string and reverse it by adding it to the front of the string:

And the results:

Image 7 - Palindrome check test results

Image 7 – Palindrome check test results

The alternative approach would be to reverse the string using Python’s slicing notation ([::-1]), or by using the reversed() functions – but these are Python-specific and likely don’t apply to other programming languages.

Question #7 – Calculate a Factorial Using Recursion

A factorial of a number is the product of all the integers from that number to 1. For example, the factorial of 5, or 5! is 5*4*3*2*1 = 120. Factorials are not defined for negative numbers and the factorial of zero is one.

A recursive function is a function that calls itself during its execution. It needs an exit condition, or it will run indefinitely.

Problem description: Given a positive integer x, return an integer that is a factorial of x. If a negative integer is provided, return -1. Implement the solution by using a recursive function.

So there are two edge cases – when x is either 0 or negative. In the first case, we should return 1, and in the second case, we should return -1. If x is a positive integer, we can proceed with the calculation:

Here are the test results:

Image 8 - Factorial test results

Image 8 – Factorial test results

There are built-in functions for factorial calculations, but you shouldn’t rely on those if you want to clear a technical interview. Going through every step manually is the way to go.

How are your data visualization skills? Solidify them with these data visualization best practices.

Data Science Coding Interview Tips

We’ve covered seven beginner-friendly data science coding interview questions and answers. It’s enough for today, but we want to leave you with some bonus tips to ace your first technical interview:

  • State and restate the problem clearly – Don’t start writing code until you’re certain you can solve it. There’s nothing worse than figuring out your solution won’t work after writing 90% of the code.
  • Come up with a bunch of test cases – Aim to cover all the edge cases you can think of. It’s important to walk the interviewer through your thought process.
  • Explain your solution in plain English – Even before you start coding it – the simplest solution is most likely the correct one.
  • Write pseudocode first – Simply to fact check your logic and save time if there are errors in your thought process.

Conclusion

Technical interviews can be brutal, especially if you’re applying for your first job. You should take a few weeks/months to go through the most common interview questions and answers, and make sure to understand how a particular solution works.

We’ve only covered bits of programming interview questions and answers, but that alone isn’t enough for a data scientist. You’re also expected to be proficient in databases and other data science/machine learning skills. We’ll cover some of the most common interview questions for data science in a future article, so stay tuned.

What did your first technical interview look like? Did you get the job? What are the tips and tricks you can give to beginners? Share in the comment section below to let others know what to expect.

Curious what skills do you need to get hired in data science? Read our complete rundown for 2022 and beyond.