My first instinct was to just iterate through every number and count the 1s.
Start by clarifying the problem and edge cases, then discuss a brute-force approach before optimizing. For an optimal solution, derive a digit-by-digit formula that counts occurrences of '1' at each place value (units, tens, hundreds, etc.) in O(log n) time.
Pro tip: Demonstrate strong problem-solving by walking through a concrete example (e.g., n=314) to validate your formula, and mention that this pattern generalizes to counting any digit. This shows attention to detail and scalability.
Confirm the range (1 to n inclusive), handle n <= 0, and discuss constraints (e.g., n up to 10^9).
Mention iterating from 1 to n and counting '1's in each number, which is O(n log n) and may be too slow for large n.
For each place value (factor = 1, 10, 100, ...), compute how many times '1' appears at that position using higher and lower digits.
Write code that iterates over place values, applies the formula, and test with small cases (e.g., n=13) and edge cases (n=0, n=1).
State that the solution runs in O(log n) time and O(1) space, which is optimal for large n.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.