FastPrepSentence Equivalence with Synonyms
Problems/Sentence Equivalence with Synonyms

Sentence Equivalence with Synonyms

Read AI logoRead AI● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Read AI phone screen. The report gives synonym pairs, two sentences, a true or false answer on whether they mean the same, and a follow-up where synonyms chain together. We made these clear: synonyms work both ways, words are compared by position, what happens to words with no match, and the input limits.

You are given two sentences, firstSentence and secondSentence, represented as ordered arrays of words, together with an array of synonym pairs.

Each synonym pair is undirected, and synonym relationships are transitive. For example, if rapid is a synonym of fast and fast is a synonym of quick, then rapid and quick are also equivalent.

Return true exactly when the sentences have equal length and every pair of words at the same position is either identical or belongs to the same synonym component. A word absent from the synonym graph matches only itself.

Function

areSentencesEquivalent(firstSentence: String[], secondSentence: String[], synonymPairs: String[][]) → boolean

Examples

Example 1

firstSentence = ["flat","is","useful"]secondSentence = ["flatting","is","useful"]synonymPairs = [["flat","flatting"]]return = true

The first aligned words are directly connected by a synonym pair, and the remaining aligned words are identical.

Example 2

firstSentence = ["hit","the","ball"]secondSentence = ["hits","a","ball"]synonymPairs = [["hit","hits"]]return = false

Although hit and hits are connected, the and a are neither identical nor synonyms.

Example 3

firstSentence = ["rapid","meeting"]secondSentence = ["quick","session"]synonymPairs = [["rapid","fast"],["fast","quick"],["meeting","session"]]return = true

rapid reaches quick transitively through fast, while meeting and session are directly connected.

Constraints

  • 0 <= firstSentence.length, secondSentence.length <= 100000.
  • 0 <= synonymPairs.length <= 100000.
  • Every synonym pair contains exactly two words.
  • Every word is a nonempty lowercase English token.
  • The total number of sentence words plus synonym-pair endpoints is at most 200000.

Similar questions at other companies

  • Record Linkage Part 3 - Full Connected ComponentStripe · PHONE SCREEN
  • Count Distinct Word MeaningsDuolingo · OA
  • Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREEN
  • Nested Substitutions with Cycle DetectionKikoff · PHONE SCREEN
  • Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEW
  • Group Sparse Points by Distance ThresholdNuro · PHONE SCREEN
  • Group Transitive Synonyms by Four-Token ContextGlean · PHONE SCREEN
  • Largest Tree Size in a ForestGoldman Sachs · PHONE SCREEN
  • Resolve Variable Equations with Dependency ErrorsMotive · PHONE SCREEN
  • Resource Transition ProbabilitiesWhatNot · PHONE SCREEN
See Read AI hiring insights
Saved
class Solution {
    public boolean areSentencesEquivalent(String[] firstSentence, String[] secondSentence, String[][] synonymPairs) {
      // write your code here
    }
}
Checking account…