Perfect Squares
MediumDynamic Programming1d dp
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 neededProblem
Given a positive integer n, return the smallest count of perfect squares (such as 1, 4, 9, 16, ...) whose sum is exactly n. The same square may be used more than once.
Since 1 is a perfect square, an answer always exists.
Examples
Input: n = 12
Output: 3
12 = 4 + 4 + 4, and no two squares sum to 12.
Input: n = 13
Output: 2
13 = 4 + 9.
Constraints
- 1 <= n <= 10^4
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.