← Amazon Interview Insights

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

Intermediate
Apr 2026

Summary

Amazon OA for a SWE role. One coding problem about finding substrings in a purchase history string that match a permutation of a given product list. Pretty classic sliding window / anagram-style problem dressed up in a product recommendation story.

Questions Asked (1)

Q1

Given a string representing a customer's purchase history and an array of equal-length product strings, count how many substrings of the history can be formed by concatenating all the products in any order (each product used exactly once, including duplicates).

Algorithms & Data Structures
Author's notes

Took me a minute to see past the product recommendation wrapper and realize this is just a sliding window frequency count problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: we need to count substrings of the history string that are permutations of the concatenation of all product strings (with duplicates). Then, use a sliding window approach where the window size is fixed to the total length of all products, and maintain frequency counts of characters in the window and the target multiset. For each window, compare the frequency maps to check if it's a valid permutation, and count matches.

Pro tip: Precompute the total length and character frequency of the concatenated products to avoid recalculating, and use an array of size 26 for frequencies to achieve O(1) comparison via a match counter. This optimizes the solution to O(n) time and demonstrates strong algorithmic maturity.

1. Clarify and Validate

Confirm that products can be in any order, duplicates are allowed, and each product must be used exactly once. Check edge cases: empty products, empty history, or total length exceeding history length.

2. Precompute Target Frequencies

Concatenate all product strings and compute the frequency of each character (e.g., using a hash map or array of size 26). Also compute the total length L of the concatenated string.

3. Sliding Window with Frequency Matching

Initialize a window of size L over the history string. Maintain character frequencies of the window and a match count of how many characters have the correct frequency. Slide the window one character at a time, updating frequencies and match count, and count windows where match count equals the number of distinct characters in the target.

4. Handle Edge Cases and Return Count

If L is 0, return 0 (or handle as per definition). If L > history length, return 0. Otherwise, return the count of valid windows.

Key Points to Mention

  • Sliding window technique for fixed-size substrings
  • Character frequency counting and comparison (using arrays or hash maps)
  • Optimization using a match counter to achieve O(1) comparison per window
  • Time complexity: O(n) where n is the length of the history string
  • Space complexity: O(1) if using fixed-size frequency arrays (e.g., 26 for lowercase letters)
  • Handling duplicates in products by including them in the frequency count

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