Power of Three

EasyMathdivision loop

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer n, determine whether it is a power of three. An integer is a **power of three** if there exists some non-negative integer exponent x such that n == 3^x. The valid powers of three therefore begin 1 (which is 3^0), 3, 9, 27, 81, and so on. Return true if n is a power of three, and false otherwise. Note that zero and all negative integers are never powers of three.

Examples

Input: n = 27
Output: true
27 equals 3^3, so it is a power of three.
Input: n = 0
Output: false
There is no exponent x for which 3^x equals 0, and powers of three are always at least 1.
Input: n = 45
Output: false
45 factors as 3 * 3 * 5. The leftover factor of 5 means it cannot be written purely as a power of three.

Constraints

  • -2^31 <= n <= 2^31 - 1
  • The input is a single 32-bit signed integer.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.