Top 7 Data Science Coding Interview Questions and Answers for 2022

By:
Dario Radečić
February 15, 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.
<h2>Data Science Coding Interview Questions and Answers</h2>
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.
<blockquote><a href="https://appsilon.com/dash-vs-shiny/" target="_blank" rel="noopener noreferrer">Python Dash vs R Shiny</a> - Learn which is better for your use case.</blockquote>
Table of contents:
<ul><li><a href="#question-1">FizzBuzz</a></li><li><a href="#question-2">Reverse an Integer</a></li><li><a href="#question-3">Find the First Unique Character in a String</a></li><li><a href="#question-4">Defanging an IP Address</a></li><li><a href="#question-5">Check if a String is an Anagram</a></li><li><a href="#question-6">Check if String is a Palindrome</a></li><li><a href="#question-7">Calculate a Factorial Using Recursion</a></li><li><a href="#tips">Data Science Coding Interview Tips</a></li><li><a href="#conclsuion">Conclusion</a></li></ul>

<hr />

<h2 id="question-1">Question #1 - FizzBuzz</h2>
This one is a classic and good practice for beginners.

<strong>Problem description</strong>: Given an integer <code>n</code>, return a string array <code>result</code> where:
<ul><li><code>result[i] == “FizzBuzz”</code> if <code>i</code> is divisible by 3 and 5.</li><li><code>result[i] == “Fizz”</code> if <code>i</code> is divisible by 3.</li><li><code>result[i] == “Buzz”</code> if <code>i</code> is divisible by 5.</li><li><code>result[i] == i</code> in any other case.</li></ul>
You should check for the <em>FizzBuzz</em> condition first, as it checks for multiple conditions. For example, the number 15 is divisible with both 3 and 5, so <em>FizzBuzz</em> should get printed. 15 is also divisible by 3 and 5 individually, and we don’t want <em>Fizz</em> or <em>Buzz</em> printed alone.

Here’s a solution:

<script src="https://gist.github.com/darioappsilon/3558b1d9fcc31aa5196e1142f1e3f8cf.js"></script>

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

<img class="size-full wp-image-12131" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01edbd6bd062d7ff7dfa9_FizzBuzz-test-cases-output.webp" alt="Image 1 - FizzBuzz test cases output" width="2534" height="144" /> 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 <code>assert</code> statement.
<blockquote>Are coding interview questions giving you anxiety? Take a break and learn how to <a href="https://appsilon.com/r-shiny-dashboard-templates/" target="_blank" rel="noopener noreferrer">develop an R Shiny Dashboard in 10 minutes</a>.</blockquote>
<h2 id="question-2">Question #2 - Reverse an Integer</h2>
Yet another simple programming interview question with a couple of interesting edge cases.

<strong>Problem description</strong>: Given a signed (positive or negative) integer <code>x</code>, return <code>x</code> with its digits reversed. The input integer <code>x</code> must be in range <code>[-2^31, 2^31 - 1]</code> - 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 <code>1200</code> should result in <code>21</code>, not <code>0021</code>.

Here’s a solution:

<script src="https://gist.github.com/darioappsilon/a255ca09fa9233a768858068fd4ff17c.js"></script>

And here’s the output from the test cases:

<img class="size-full wp-image-12133" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01edd83f54e0b0d33e76f_integer-reversal-test-cases-output.webp" alt="Image 2 - Integer reversal test cases output" width="586" height="234" /> 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 (<code>[::-1]</code>), 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:

<script src="https://gist.github.com/darioappsilon/5b49b061e88fc5527f8e8f00f975a123.js"></script>

<img class="size-full wp-image-12123" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01eddfeabe8f16c326acd_assertion-error-on-integer-reversal.webp" alt="Image 3 - Assertion error on integer reversal" width="2180" height="626" /> 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.
<blockquote>Data science is more than extracting knowledge. Learn how to share your data insights <a href="https://appsilon.com/ux-design-of-shiny-apps-7-steps-to-design-dashboards-people-love/" target="_blank" rel="noopener noreferrer">using designs people will love</a>.</blockquote>
<h2 id="question-3">Question #3 - Find the First Unique Character in a String</h2>
It’s a common question that will assess your basic programming skills.

<strong>Problem description</strong>: Given a string <code>s</code>, 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 <em>A</em> and <em>a</em> are identical:

<script src="https://gist.github.com/darioappsilon/3dac1d973f1ae99666135ccfd8142f83.js"></script>

Here’s the result from our test cases:

<img class="size-full wp-image-12129" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01edf9506383404c4df76_first-unique-character-indices.webp" alt="Image 4 - First unique character indices" width="220" height="144" /> Image 4 - First unique character indices

The first test string was “Appsilon”, and the first letter <em>A</em> occurs only once. In the string “Appsilon Poland”, both <em>A</em> and <em>p</em> are repeated, and the first unique letter is <em>s</em>. As for the last string, no character occurs only once, so -1 is returned.
<h2 id="question-4">Question #4 - Defanging an IP Address</h2>
Your localhost IP address is <code>127.0.0.1</code>. The process of defanging basically surrounds all the dots with brackets. The end result is <code>127[.]0[.]0[.]1</code>. Easy!

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

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:

<script src="https://gist.github.com/darioappsilon/6ecba198ce9e873ae22fa17032e32592.js"></script>

Here’s the test case output:

<img class="size-full wp-image-12125" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ee1cbd83bfce97b570c_defanged-IP-address-test-results.webp" alt="Image 5 - Defanged IP address test results" width="754" height="96" /> Image 5 - Defanged IP address test results

The alternative way would be to use Python’s <code>replace()</code> function to replace every dot with our custom sign, but that’s something you can do on your own.
<h2 id="question-5">Question #5 - Check if a String is an Anagram</h2>
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.

<strong>Problem description</strong>: Given two strings <code>a</code> and <code>b</code>, return <code>True</code> if <code>b</code> is an anagram of <code>a</code>, and <code>False</code> 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:

<script src="https://gist.github.com/darioappsilon/a53f0e33c73961a597c0a3691fbf4f07.js"></script>

Here are the results:

<img class="size-full wp-image-12121" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ee24d7961f8266fe398_anagram-check-test-results.webp" alt="Image 6 - Anagram check test results" width="870" height="132" /> Image 6 - Anagram check test results

Our logic works! Sure, there are more Pythonic ways to check for an anagram, such as using the <code>sorted()</code> or <code>set()</code> 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.
<h2 id="question-6">Question #6 - Check if String is a Palindrome</h2>
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.

<strong>Problem description</strong>: Given a string <code>s</code>, return <code>True</code> if it’s a palindrome, or <code>False</code> 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:

<script src="https://gist.github.com/darioappsilon/f8ab12786b1cf028f48e258ea99ef5c0.js"></script>

And the results:

<img class="size-full wp-image-12135" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ee42a228de4b21e975f_palindrome-test-results.webp" alt="Image 7 - Palindrome check test results" width="1054" height="172" /> Image 7 - Palindrome check test results

The alternative approach would be to reverse the string using Python’s slicing notation (<code>[::-1]</code>), or by using the <code>reversed()</code> functions - but these are Python-specific and likely don’t apply to other programming languages.
<h2 id="question-7">Question #7 - Calculate a Factorial Using Recursion</h2>
A factorial of a number is the product of all the integers from that number to 1. For example, the factorial of 5, or <code>5!</code> is <code>5*4*3*2*1 = 120</code>. 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.

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

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

<script src="https://gist.github.com/darioappsilon/ba09d3c7aa3e9cdd4fafdb14ff5abe24.js"></script>

Here are the test results:

<img class="size-full wp-image-12127" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ee5a8912184dd39d476_factorial-test-results.webp" alt="Image 8 - Factorial test results" width="370" height="262" /> 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.
<blockquote>How are your data visualization skills? Solidify them with these <a href="https://appsilon.com/data-visualization-best-practices-bar-plots/" target="_blank" rel="noopener noreferrer">data visualization best practices</a>.</blockquote>
<h2 id="tips">Data Science Coding Interview Tips</h2>
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:
<ul><li><strong>State and restate the problem clearly</strong> - 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.</li><li><strong>Come up with a bunch of test cases</strong> - Aim to cover all the edge cases you can think of. It’s important to walk the interviewer through your thought process.</li><li><strong>Explain your solution in plain English</strong> - Even before you start coding it - the simplest solution is most likely the correct one.</li><li><strong>Write pseudocode first</strong> - Simply to fact check your logic and save time if there are errors in your thought process.</li></ul>

<hr />

<h2 id="conclusion">Conclusion</h2>
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.

<em>What did your first technical interview look like? Did you get the job? What are the tips and tricks you can give to beginners?</em> Share in the Shiny 4 All community on Slack to let others know what to expect.
<blockquote>Curious what skills do you need to get hired in data science? Read our <a href="https://appsilon.com/top-7-data-science-skills-2022/" target="_blank" rel="noopener noreferrer">complete rundown for 2022 and beyond</a>.</blockquote>

Have questions or insights?

Engage with experts, share ideas and take your data journey to the next level!

Is Your Software GxP Compliant?

Download a checklist designed for clinical managers in data departments to make sure that software meets requirements for FDA and EMA submissions.

Sign up for ShinyWeekly

Join 4,2k explorers and get the Shiny Weekly Newsletter into your mailbox
for the latest in R/Shiny and Data Science.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Explore Possibilities

Share Your Data Goals with Us

From advanced analytics to platform development and pharma consulting, we craft solutions tailored to your needs.

Talk to our Experts
python
career
community
data science
ai&research