Magicsheet logo

Sentence Similarity III

Medium
12.5%
Updated 8/1/2025

Sentence Similarity III

What is this problem about?

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.

Why is this asked in interviews?

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.

Algorithmic pattern used

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.

Example explanation

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.

Common mistakes candidates make

  • Not using the shorter sentence's length as the threshold — the insertion gap must consume all unmatched words from the longer sentence, leaving all of the shorter sentence matched.
  • Matching from both ends without tracking which sentence is shorter.
  • Confusing "single contiguous gap" with "multiple scattered insertions" — only one gap is allowed.
  • Off-by-one when the pointers from left and right overlap.

Interview preparation tip

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.

Similar Questions