← Amazon Interview Insights

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

Intermediate
May 2026

Summary

Amazon SWE coding round with a string manipulation problem that's trickier than it looks on the surface. The binary search angle took me a while to see.

Questions Asked (1)

Q1

You're given a string S, an offset array O that defines the order in which characters get replaced by '*', and an integer M. Each round you replace S[O[i]] with '*'. What's the minimum number of rounds required so that the number of substrings containing at least one '*' reaches at least M?

Algorithms & Data Structures
Author's notes

My first instinct was to simulate it round by round and count substrings each time, which obviously blows up for large inputs.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that the number of substrings containing at least one '*' can be computed as total substrings minus substrings with no '*'. After each replacement, the string splits into segments of non-'*' characters; the count of substrings without '*' is the sum of len*(len+1)/2 for each segment. Use a data structure to efficiently update segment lengths as positions are replaced, and binary search or simulate rounds until the count reaches M.

Pro tip: Clarify with the interviewer whether the offset array O contains distinct indices and covers all positions; this affects whether you can binary search the answer or must simulate. Also, mention that you can precompute the total number of substrings and track the reduction in non-star substrings to avoid recomputing from scratch.

1. Understand the problem and define the metric

Restate the problem: after each round, replace S[O[i]] with '*'. We need the minimum rounds so that the number of substrings containing at least one '*' is >= M. Define the count as total substrings - substrings without '*'.

2. Derive the formula for substrings without '*'

When the string is divided into contiguous segments of non-'*' characters, the number of substrings without '*' is the sum over segments of L*(L+1)/2, where L is the segment length. Initially, if there are no '*', the whole string is one segment.

3. Choose an efficient data structure

Use a balanced BST (e.g., TreeSet in Java) or a sorted list to store the positions of '*' (or the boundaries of segments). When a new position is replaced, find the segment containing it, split it into two, and update the total count by subtracting the old segment's contribution and adding the two new segments' contributions.

4. Determine the minimum rounds

Since the count of substrings with '*' is monotonically non-decreasing as more positions become '*', we can either simulate round by round (O(n log n) total) or binary search on the number of rounds if we can quickly compute the count after k rounds. For simulation, after each round check if count >= M and return the round number.

5. Handle edge cases and optimize

Consider cases where M is 0 (answer 0), M exceeds total substrings (impossible, return -1 or handle as per problem), and when all characters become '*' (count = total substrings). Ensure the data structure supports efficient insertion and predecessor/successor queries.

Key Points to Mention

  • Total number of substrings is n*(n+1)/2.
  • Number of substrings without '*' is sum of L*(L+1)/2 for each contiguous non-'*' segment.
  • Use a balanced BST or sorted set to maintain '*' positions and quickly find segment boundaries.
  • Update the count incrementally: when splitting a segment of length L into L1 and L2, subtract L*(L+1)/2 and add L1*(L1+1)/2 + L2*(L2+1)/2.
  • The count is monotonic, so simulation round-by-round is O(n log n) and sufficient; binary search is possible if you can compute count after k rounds efficiently.
  • Edge cases: M=0, M > total substrings, and all positions replaced.

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