← Amazon Interview Insights

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

Intermediate
Jun 2026

Summary

Amazon SWE coding round, one problem the whole time. It was dressed up in a customer behavior story but stripped down it's just a substring counting problem with a specific constraint.

Questions Asked (1)

Q1

Given a binary string representing a user's action history, count all substrings where the number of '0's equals the square of the number of '1's.

Algorithms & Data Structures
Author's notes

The Amazon framing about 'major vs minor actions' threw me off for a second because I kept trying to make the business context mean something for the solution.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases, then propose an efficient algorithm using prefix sums and hashing to count valid substrings in O(n) time. Explain the transformation of the condition into a prefix sum equation and how to handle the square relationship.

Pro tip: Discuss the time and space complexity trade-offs and mention how you would test the solution with edge cases like empty string or all zeros. This shows you consider production quality and not just correctness.

1. Understand the problem

Restate the problem in your own words and ask clarifying questions about input size, character set, and expected output. Confirm that substrings are contiguous and that we count all occurrences.

2. Brute force approach

Mention that a naive O(n^2) solution checks all substrings and counts zeros and ones, but it's inefficient for large n. This sets the stage for optimization.

3. Optimize with prefix sums

Define prefix counts of zeros and ones. For a substring from i to j, the condition is (Z_j - Z_i) = (O_j - O_i)^2. Rearrange to find a relationship between prefix sums that can be checked efficiently.

4. Use hashing to count

Iterate through the string, maintaining counts of zeros and ones. For each position, compute the required value based on the equation and use a hash map to count how many previous prefixes satisfy it.

5. Analyze complexity and edge cases

State that the algorithm runs in O(n) time and O(n) space. Discuss edge cases: empty string, no valid substrings, all zeros, all ones, and large inputs.

Key Points to Mention

  • Prefix sum technique for counting substrings with specific properties
  • Hash map to store frequencies of prefix sum combinations
  • Time complexity O(n) and space complexity O(n)
  • Handling of edge cases such as empty string and strings with no valid substrings
  • Derivation of the condition: zeros = ones^2, and how it translates to prefix sums
  • Potential integer overflow when squaring the number of ones, especially for large substrings

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