How do you check if a number is a power of another number?

Coding
Medium
Infosys
97.6K views

This question asks for an algorithm to verify exponentiation relationships. It tests logarithmic checks or iterative division.

Why Interviewers Ask This

Interviewers ask this to test mathematical logic and the ability to handle edge cases like 0, 1, and negative bases. They want to see if candidates can use logarithms or repeated division effectively.

How to Answer This Question

Explain repeatedly dividing the number by the base until it equals 1 or the division leaves a remainder. If it reaches 1, it is a power. Alternatively, use logarithms: if log(base)(num) is an integer, it is a power. Discuss precision issues with logs.

Key Points to Cover

  • Repeated division
  • Remainder check
  • Logarithmic alternative
  • Edge case handling

Sample Answer

To check if a number is a power of another, I can repeatedly divide the number by the base. If the number eventually becomes 1 and all divisions had no remainder, it is a power. For example, 8 divided by 2 three times be…

Common Mistakes to Avoid

  • Ignoring remainder
  • Floating-point inaccuracies
  • Not handling base 1

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 80 Coding questionsBrowse all 149 Infosys questions