4 Python Algorithms for Coding Interviews And Their Answer

3 min read

Photo by Headway on Unsplash

Introduction

Whether you want to be a Data Scientist, Software Developer, or any position that includes coding, you might be going to face with a Coding challenge.

Learning Coding is like learning a new language. You have to develop a neural path in your mind in that language.

For example, during learning a new language, there are stages.

  • Vocabulary
  • Listening
  • Writing
  • Speaking

You have to be proficient in four of that stages to understand clearly and speak fluently.

In python, the schedules, of course, are different.

  • Data Types and Operators
  • Data Structures
  • Control Flows
  • Conditional Statements
  • List Comprehension
  • Loops
  • Functions
  • Object Oriented Programming
  • Data Structures And Algorithms

And many advanced topics.

If you want to speak Python, you have to be proficient in these topics first.

One of the main things to do for me is solve coding challenges. Because that will help you to boost your coding challenge in your Interview too.

Where?

Here is 4 website for you to sharpen your coding skills.

  • Hackerrank
  • Dataquest
  • Machinehack
  • Edabit

If you want to see more details, you can read that article.

1. Numpy Array Shaper

Write a code, that will take 9 numbers as an input and turns it into an array, 3×3 shape.

Now after that explanation, almost anytime they either describe or give an explanation about sample Input and sample Output.

Sample Input

1 2 3 4 5 6 7 8 9

Sample Output

Image by Author

Answer

Here is the answer;

Now, let us load the library and take the input.

Decoding Steps

First, we should split the given input, and second turn them into numbers.

Third, we will change the type as an array.

Fourth, we will reshape that array.

For more Numpy functions;

2. Leap Year Finder

Now, in this challenge, we will determine the given year, whether it is a leap year or not.

If it will be a leap year the code should return True or return False.

The leap year conditions;

  • If the year can be evenly divided by 4, it is a leap year.
  • However, one additional rule, if the year can be evenly divided by 4 and 100, it is not a leap year, unless the year is also evenly divided by 400.

Let me give an example;

  • 1500, 1700 both can be evenly divided by 4 and 100 but not 400, so they do not leap years.
  • On the other hand, 1600 is.

Now, it is obvious we will write a function containing if else block.

Before looking for the answer, try to write your own by yourself, it actually is like a game and rather amusing.

Answer

Here is the answer.

Decoding Steps

For this challenge, 4 blocks of if-elif are needed.

First, if the number can be evenly divided by ;

  • 4 but not 100.
  • Second 4 and 100 and 400.
  • Third 4 and 100 but not 400.
  • Fourth not 4 either.

Takeaway

  • When solving that kind of challenge, it is important to divide problem pieces and handle one problem at a time.

3. Prime Find

Prime numbers are whole numbers that have only two dividers: 1 and the number itself. 

The first four prime numbers are 2, 3, 5, and 7.

For example, 8 has four dividers: 1, 2, 4, 8. 

  • 1 X 8= 6 
  • 2 X 4= 8

So we know 8 is not a prime number.

Now, we will write code to check if the numbers in the list are prime numbers or not.

If the numbers are prime, the code should print

* “ ( )is a prime number.”

If the number is not a prime number, it should print 

  • “( ) is not a prime number, because ( ) is a divider of ()

Answer

Here is the answer.

Decoding

Now to solve this answer we need two loops.

If our first loop can divide our second loop, the number automatically is not a prime because it has a divider, hence we will print;

is not a prime number because { } is a divider of { }.

We should fill the black correctly with the divider and our number.

If our first block does not answer, then our number has not any divider, our second loop checks if a possible divider comes near our number.

4. List Comprehension

Suppose you are a teacher and you add the notes of your student in your dictionary and you want to find the student’s name whose grade is over 90.

Answer

Here is the answer.

Decoding

Select the name in that dictionary item, if the score is greater and equal to 90.

Conclusion

Now, of course, the list goes on and on.

One other tip for you, if you already study one Python library or concept, you can filter the challenges in Hackerrank, then focus on that one.

On the other hand, if you are at the bottom of the ladder, first try to learn all of the pre-requisites that I gave to you as a list in the Intro.

And one additional piece of information comes from my experience if you want to be really good at any one of the programming languages you should write code every day. 

In the first days of learning coding days, I really had a struggle doing that but try finding an amusing way to code.

Personal projects and coding websites really help me be consistent.

You should be consistent to build a neural path in your mind, and make repetition.

Repetition is the mother of learning, the father of action, which makes it the architect of accomplishment." Zig Ziglar

Thanks for reading my article.

“Machine learning is the last invention that humanity will ever need to make.” Nick Bostrom

Subscribe to me for reading articles like that, free e-books, cheatsheets, and many more.

Gencay I. Machine Learning & Mechanical Engineer | Technical Content Writer | For free Cheat sheet 📝 👉 https://gencay.ck.page/

Leave a Reply

Your email address will not be published.