4 Python algorithms for coding interviews

And their response

picture by Progress on Unsplash

Whether you want to be a Data Scientist, Software Developer, or any other position that includes coding, you may face a coding challenge.

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

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

  • Vocabulary
  • Listen
  • Writing
  • Speaking

You must master four of these steps to understand clearly and speak fluently.

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

  • Data types and operators
  • Data structures
  • Control flow
  • Conditional Expressions
  • Comprehension of the list
  • Loops
  • Functions
  • object-oriented programming
  • Data structures and algorithms

And many advanced topics.

If you want speak Pythonyou must first master these subjects.

One of the main things for me is fixing coding issues. Because it will also help you with your coding challenge in your interview.

Where?

Here are 4 websites to hone your coding skills.

  • hackerrank
  • Data search
  • Machinehack
  • Edabit

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

Content Table· Introduction
·
1. Numpy Array Shaper
∘ Sample Input
∘ Sample Output
∘ Answer
∘ Decoding Steps
· 2. Leap Year Finder
∘ Answer
∘ Decoding Steps
∘ Takeaway
· 3. Prime Finder
∘ Answer
∘ Decoding
· 4. List Comprehension
∘ Answer
∘ Decoding
· Conclusion

Write a code, which will take 9 numbers as input and transform it into an array of 3×3 shape.

Now after this explanation almost every time they describe or give explanation about input sample and output sample.

Sample entry

1 2 3 4 5 6 7 8 9

Example output

Author’s picture

Answer

Author’s picture

Now let’s load the library and take the input.

Decoding steps

First, we need to divide the given input and then turn them into numbers.

Third, we’ll change the type as an array.

Fourth, we will reshape this table.

For more Numpy functions;

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

If it is a leap year, the code should return True or False.

leap year conditions;

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

Let me give you an example;

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

Now it is obvious that we are going to write a function containing the if else block.

Before looking for the answer, try to write your own, it’s actually like a game and quite fun.

Answer

Author’s picture

Decoding steps

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

First, if the number can be divided by;

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

Carry

  • When solving this type of challenge, it is important to split the pieces of the problem and deal with one problem at a time.
picture by Jean-Carlo Tubelleza on Unsplash

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

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

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

So we know that 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 primethe 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 divisor of ()

Answer

Author’s picture

Decoding

Now to solve this answer we need two loops.

If our first loop can divide our second loop, the number automatically isn’t a prime because it has a divisor, so we’ll print;

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

We need to correctly fill the black with the separator and our number.

If our first block does not respond, then our number has no divisor, our second loop checks if a possible divisor comes close to our number.

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

Answer

Decoding

Select the noun in this dictionary item, if the score is greater than and equal to 90.

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

Another tip for you, if you are already studying a Python library or concept, you can filter the challenges in Hackerrank and then focus on that one.

On the other hand, if you are at the bottom of the scale, first try to learn all the prerequisites that I gave you in the form of a list in the Intro.

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

In the first few days of learning to code, I really struggled, but try to find a fun way to code.

Personal projects and coding websites really help me be consistent.

You have to be consistent to build a neural pathway in your mind and do reps.

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 humanity will ever need to make.” Nick Bostrom

Sharon D. Cole