Smallest Integer Divisible by K

MediumMathmath

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

A *repunit* is a positive integer whose decimal digits are all 1 (such as 1, 11, 111, ...). Given a positive integer k, find the shortest repunit that is divisible by k and return its number of digits. If no repunit is divisible by k, return -1. The repunit itself may be far too large for a 64-bit integer, so work with remainders.

Examples

Input: k = 1
Output: 1
1 is divisible by 1.
Input: k = 2
Output: -1
Every repunit is odd, so none is divisible by 2.
Input: k = 3
Output: 3
111 = 3 * 37 is the shortest repunit divisible by 3.

Constraints

  • `1 <= k <= 10^5`

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.

More math problems