The Sentence Similarity III interview question gives you two sentences and asks whether one can be made equal to the other by inserting words at the beginning, end, or a single continuous gap in the middle. The idea is that one sentence can "extend" the other by adding words in one contiguous region. This is a two-pointer matching problem on word arrays.
Meta, Amazon, TikTok, Google, and Bloomberg ask this problem because it tests two-pointer or deque-based matching with a clear geometric interpretation: both sentences must share a common prefix and common suffix, with all extra words concentrated in one contiguous middle section. It evaluates whether candidates can translate the "one insertion gap" constraint into a precise algorithmic condition.
The pattern is two-pointer prefix and suffix matching. Convert both sentences to word arrays. Use a left pointer to match common prefix words from the front. Use a right pointer to match common suffix words from the back. The pointers stop when words differ. For one sentence to be similar to the other via a single insertion, the shorter sentence's words must all be matched — either as prefix, suffix, or a combination where the total matched prefix+suffix equals the shorter sentence's length.
sentence1: "My name is John" → words1 = ["My", "name", "is", "John"]
sentence2: "My full name is John" → words2 = ["My", "full", "name", "is", "John"]
Left pointer: "My"=="My" → advance. Left=1. "name"!="full" → stop. Right pointer: "John"=="John" → advance. Right offset=1. "is"=="is" → advance. Right offset=2. "name"!="full" → stop.
Matched: left=1 prefix + 2 suffix = 3 total from words1 (length 4)... 1+2=3 < 4. Hmm. Adjust: 1 prefix + 3 suffix (is, John... wait). Re-check: after "My" matches, from right: John=John, is=is, name=full? No. So right matches = 2. For words1 (shorter, length 4): left(1) + right(2) = 3 < 4. Not valid. But sentence1 = ["My","name","is","John"], sentence2=["My","full","name","is","John"]: left=1 (My), right=3 (name,is,John), 1+3=4=len(words1) → true.
For the Sentence Similarity III coding problem, the two-pointer string array interview pattern is the right approach. The key insight: find the longest matching prefix and suffix; if their combined length covers the entire shorter sentence, it's similar. Meta and Bloomberg interviewers appreciate when you explain the invariant before coding. Practice on edge cases: one sentence is empty, sentences are identical, or all extra words are at the end.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Expressive Words | Medium | Solve | |
| Find First Palindromic String in the Array | Easy | Solve | |
| Shortest Distance to a Character | Easy | Solve | |
| Check If String Is a Prefix of Array | Easy | Solve | |
| Adding Spaces to a String | Medium | Solve |