Member-only story

Interview question: Replace all odd numbers in an array with -1 using Python

Crystal X
2 min readJul 20, 2023

--

There are so many questions that can be asked at a coding interview a person can be unsure of what to study. The best option is to become familiar with as many algorithms as possible and to thoroughly understand Python’s operators.

One operator that is crucial to the Python programming language is the modulo operator and is represented by the % symbol. It returns the remainder of dividing the left hand operand by right hand operand, and is used to get the remainder of a division problem.

For example: 17 % 12 = 5

The modulo operator is used quite frequently in Python programming, and one question that can come up in a coding interview that can use the modulo operator, is to replace all odd numbers in an array with a -1.

The solution to the interview question can be seen in the pseudocode below:-

  1. Define a list of numbers, lst
  2. Define a blank list, new_lst.
  3. Create a for loop that will iterate through the length of lst. Define the variable, ltr, which is the value of the iterated position lst is in. If ltr modulo 2 equals 0 then lst equals the iterated position of lst, otherwise lst equals -1. At each iteration, append ltr to the list, new_lst.

As an exercise, you can change the interview question to replace all even numbers in an array with -1. The code to such a question can be seen below:-

--

--

Crystal X
Crystal X

Written by Crystal X

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.

No responses yet