← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Apr 2026

Summary

Amazon SWE coding round with a pretty gnarly string/virus problem. The constraints were large enough that a naive O(n^2) approach was never going to fly, so you had to think carefully about how substrings with at least one '*' accumulate over time.

Questions Asked (1)

Q1

You have a password string of length n and an attack order array (a permutation of 1..n). At each second i, the character at position attackOrder[i] gets replaced by '*'. The password becomes irrecoverable once the number of substrings containing at least one '*' reaches or exceeds m. Find the minimum time t after which the password is irrecoverable. If it's already irrecoverable before any attack, return 1. Constraints: n up to 8e5, m up to n*(n+1)/2.

Algorithms & Data Structures
Author's notes

This one hurt a little.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Binary search on the time t and use a data structure to efficiently compute the number of substrings containing at least one '*'. Alternatively, process attacks in reverse using a union-find to maintain segments of non-'*' characters and compute the count incrementally. The key is to achieve O(n log n) or O(n α(n)) time.

Pro tip: Emphasize that the number of substrings containing at least one '*' equals total substrings minus substrings consisting entirely of non-'*' characters. This reduces the problem to tracking contiguous segments of un-attacked positions.

1. Clarify and Define

Restate the problem: we need the earliest time when the count of substrings with at least one '*' is >= m. Note that if m is 0, answer is 1 (but constraints say m >= 1).

2. Mathematical Transformation

Express the count as total substrings minus substrings without '*'. Total substrings = n*(n+1)/2. Substrings without '*' are those entirely within contiguous blocks of un-attacked positions.

3. Choose an Efficient Algorithm

Option A: Binary search on t, and for each t, compute the sum of len*(len+1)/2 over all segments of un-attacked positions. Use a Fenwick tree or ordered set to maintain segments. Option B: Process attacks in reverse, starting with all positions un-attacked and merging segments using union-find, updating the count of substrings without '*'. Stop when the count of substrings with '*' >= m.

4. Handle Edge Cases and Complexity

If the condition is already satisfied before any attack (i.e., total substrings >= m), return 1. Ensure the algorithm runs in O(n log n) or O(n α(n)) to handle n up to 8e5.

5. Implement and Test

Write clean code, test with small cases, and verify with brute force. Consider using long long for counts since m can be up to ~3.2e11.

Key Points to Mention

  • Total substrings = n*(n+1)/2
  • Substrings without '*' are those entirely within contiguous un-attacked segments
  • Binary search on time t with segment tracking (e.g., using a Fenwick tree or ordered set)
  • Reverse processing with union-find to merge segments and maintain count of substrings without '*'
  • Time complexity: O(n log n) or O(n α(n))
  • Use 64-bit integers for counts to avoid overflow

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