← Amazon Interview Insights

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

IntermediatePrefer not to say
May 2026

Summary

Amazon SWE coding round, one problem the whole time. It was a string/permutation thing that sounds straightforward but has some real complexity hiding in it once you start thinking about how to count substrings efficiently.

Questions Asked (1)

Q1

You have a password string of length n. An attack sequence is given as a permutation of positions, and at each second t, the character at that position gets replaced with '*'. The password is considered irrecoverable once the number of substrings containing at least one '*' reaches or exceeds some threshold m. Find the minimum time t at which this happens. If it's already irrecoverable before any attack, return 1. Walk through your algorithm and give time and space complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

My first instinct was to just simulate it naively, count all substrings after each replacement, and check against m.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as counting substrings that contain at least one '*' after each attack, and find the earliest time when this count reaches m. Use a data structure like a sorted set of attacked positions to efficiently compute the number of substrings without '*' and subtract from total substrings. Alternatively, binary search on time t and check if the condition holds, using a linear scan to count substrings with '*'.

Pro tip: Clarify edge cases upfront: if m is 0 or the initial string already has enough substrings with '*' (impossible since no '*' initially), handle accordingly. Also, consider that the answer might be n+1 if never reached, but the problem likely guarantees it will be reached.

1. Understand the problem and define the metric

Restate the problem: after each attack, some positions become '*'. We need the number of substrings that contain at least one '*'. This equals total substrings minus substrings consisting only of non-'*' characters. Total substrings = n*(n+1)/2.

2. Choose an efficient approach

Since n can be large, we need an efficient way to update the count after each attack. Use a sorted set (e.g., balanced BST) to maintain the positions of '*'. When a new position is attacked, it splits an existing segment of non-'*' characters into two, reducing the count of substrings without '*' by the sum of substrings in the old segment minus the sum in the two new segments.

3. Simulate attacks and track the count

Initialize the sorted set with sentinel positions -1 and n to represent boundaries. Initially, there are no '*', so the number of substrings with '*' is 0. For each attack at position p (in the given permutation order), find the predecessor and successor in the set. The segment between them (exclusive) was all non-'*'. After inserting p, this segment splits into two. Update the count of substrings without '*' accordingly, then compute substrings with '*' = total - without. Check if it reaches m.

4. Handle the initial condition and return the answer

If the initial count (0) already >= m, return 1 (but m is likely >0). Otherwise, after each attack, if the count >= m, return the current time t (1-indexed). If after all attacks it never reaches m, return -1 or as specified (but problem implies it will).

5. Analyze complexity and discuss trade-offs

Time complexity: O(n log n) due to n insertions and predecessor/successor queries in a balanced BST. Space: O(n) for the set. Mention that a binary search approach with O(n) check per step would be O(n log n) as well but might be simpler to implement; however, the incremental approach is more direct.

Key Points to Mention

  • Total substrings formula: n*(n+1)/2.
  • Number of substrings with at least one '*' = total substrings - substrings without '*'.
  • Use a sorted set to maintain attacked positions and efficiently find segments of non-'*' characters.
  • When a new '*' is added, it splits a segment of length L into two segments of lengths a and b (a+b = L-1), reducing the count of substrings without '*' by L*(L+1)/2 - a*(a+1)/2 - b*(b+1)/2.
  • Time complexity O(n log n) and space O(n).
  • Edge cases: m=0, m > total substrings, and the initial condition.

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