Find Number of Compatible Substrings
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's substring compatibility question is a pattern-matching problem that trips up candidates who overthink the input format. You're given strings and a compatibility rule, and you need to count substrings that satisfy it. This is usually a sliding window or brute-force enumeration with string matching. If you blank on the exact rule during the OA, StealthCoder reads the problem off your screen and feeds you the solution in real time. The trick is almost always in how you define 'compatible' and whether you can optimize past O(n^2).
Pattern and pitfall
Most candidates approach this as a raw substring iteration problem: generate all substrings, check each against the compatibility condition, count matches. That works for small inputs. The hidden cost is string comparison overhead. If the compatibility rule involves character frequency, distance metrics, or pattern matching, you can optimize with a hash map or sliding window to track state as you grow the substring. Amazon loves this because it separates 'correct' from 'efficient.' The live OA will test both. If you're stuck mid-assessment, StealthCoder gives you the working code so you can move forward and not lose confidence.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Find Number of Compatible Substrings cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Number of Compatible Substrings FAQ
Is this a sliding window problem?+
Likely, yes. If 'compatible' means a property that holds as you add/remove characters from the edges, sliding window fits. If it's arbitrary substring pairs, it's pure enumeration. Read the compatibility rule carefully; it defines the approach.
How do I handle the substring enumeration efficiently?+
Start with nested loops (O(n^2) substrings). For each start index, grow the substring and update a state (hash map, frequency array). Check compatibility in O(1) or O(k) where k is alphabet size. Avoid regenerating strings; use indices and incremental state updates.
What's the compatibility rule likely to be?+
Amazon usually asks for character frequency constraints, prefix/suffix matches, or distance-based rules. Without the exact problem text, assume you'll see it on screen. The rule dictates whether you can use a fast check (like a set lookup) or need to recompute each time.
Is there a brute-force baseline I can code fast?+
Yes. Nested loops, generate each substring, check the rule naively, count. It'll pass small cases. For large inputs, optimize by maintaining a rolling window state (hash map or array) so the compatibility check stays constant-time per substring.
What if I don't understand the compatibility rule in 30 seconds?+
Reread it once more. If it's still unclear, ask the interviewer during a live OA for a clarification example. On a timed assessment, write the O(n^2) brute-force, get partial credit, and move on. Correctness beats optimization when you're low on time.