Member-only story

Interview question: Reverse a number in Python

Crystal X
2 min readJul 11, 2023

--

It is my opinion that it will soon become more difficult to obtain employment as a computer programmer with the advent of chatbots like ChatGPT. The reason for this is because those chatbots can write small programs for the more advanced programmer. In addition, programmers are now using ChatGPT as a programming assistant, which will have the effect of eliminating the need for as many junior programmers.

It is for that reason that persons aspiring to work in the field of computer programming need to hone their skills as much as possible to prepare for any future interviews.

One question that can be asked is to reverse a number. In this blog post I have written two algorithms that can easily solve the problem. The first method is to use mathematical operations and the second method is to use string operations.

Mathematical operations

The most straightforward way to solve the question of reversing a number is to:-

  1. Define the number that needs to be reversed, num.
  2. Define the number after it has been reversed and initialise it to zero, reversed_num.
  3. Create a while loop that will fun as long as num does not equal zero.
  4. In the while loop, defined the variable, digit, which is num mod 10. The…

--

--

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.

Responses (1)