← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Apr 2026

Summary

Amazon SWE coding round with a math-heavy digit counting problem. Not the kind of question you can brute force your way through at scale.

Questions Asked (1)

Q1

Given an integer n, count how many times the digit 1 appears across all integers from 1 to n.

Algorithms & Data Structures
Author's notes

My first instinct was to just iterate through every number and count the 1s.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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.

1. Clarify requirements and edge cases

Confirm the range (1 to n inclusive), handle n <= 0, and discuss constraints (e.g., n up to 10^9).

2. Propose brute-force and analyze complexity

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.

3. Derive digit-by-digit counting formula

For each place value (factor = 1, 10, 100, ...), compute how many times '1' appears at that position using higher and lower digits.

4. Implement and test with examples

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).

5. Analyze time and space complexity

State that the solution runs in O(log n) time and O(1) space, which is optimal for large n.

Key Points to Mention

  • Brute-force approach and its limitations
  • Digit-by-digit (place value) analysis
  • Formula for counting '1's at each position: (n/(factor*10))*factor + min(max(n%(factor*10)-factor+1, 0), factor)
  • Handling edge cases like n=0, n=1, and large n
  • Time complexity O(log n) and space complexity O(1)
  • Generalization to count any digit

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.