FastPrepCount Suffix Word Pairs
Problems/Count Suffix Word Pairs

Count Suffix Word Pairs

TikTok logoTikTok● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a TikTok online assessment for a summer 2027 internship. It was one of four coding questions. You count the pairs of words where one word ends with the other, and equal words count as a pair.

The report gives the full task, two examples and the limits. We added one more small example.

Our version is about 99% the same as the reported question.

Given an array of strings words, return the number of pairs of indices (i, j) with 0 <= i < j < words.length such that words[i] is a suffix of words[j], or words[j] is a suffix of words[i].

A string s is a suffix of a string t when t ends with s. Every string is a suffix of itself, so two equal strings always form a pair.

Function

countSuffixPairs(words: String[]) → long

Examples

Example 1

words = ["back","backdoor","gammon","backgammon","comeback","come","door"]return = 3

The pairs are:

  1. words[0] = "back" and words[4] = "comeback"
  2. words[1] = "backdoor" and words[6] = "door"
  3. words[2] = "gammon" and words[3] = "backgammon"

Example 2

words = ["cba","a","a","b","ba","ca"]return = 8

The pairs are:

  1. words[0] = "cba" and words[1] = "a"
  2. words[0] = "cba" and words[2] = "a"
  3. words[0] = "cba" and words[4] = "ba"
  4. words[1] = "a" and words[2] = "a"
  5. words[1] = "a" and words[4] = "ba"
  6. words[1] = "a" and words[5] = "ca"
  7. words[2] = "a" and words[4] = "ba"
  8. words[2] = "a" and words[5] = "ca"

Example 3

words = ["aa","a","aa"]return = 3

"a" is a suffix of both copies of "aa", which gives 2 pairs. The two copies of "aa" are equal, which gives 1 more pair. The answer is 3.

Constraints

  • 1 <= words.length <= 10^5
  • 1 <= words[i].length <= 10
  • words[i] consists of lowercase English letters.

More TikTok Online Assessment questions

  • Center of the Largest Perfect X-ShapeOA · Seen Nov 2026
  • Count Subarrays Matching a Comparison PatternOA · Seen Nov 2026
  • Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
  • Count Subarrays with at Least K Equal-Fruit PairsOA · Seen Oct 2026
  • Shell Command Counts with History ReferencesOA · Seen Oct 2026
  • Product Minus Sum of DigitsOA · Seen Aug 2026
  • Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
  • Find All Local PeaksOA · Seen Aug 2026
  • Count Case-Insensitive TripletsOA · Seen Aug 2026
  • Optimize TikTok Reels ViewingOA · Seen Aug 2026

Similar questions at other companies

  • Count One-Swap Number PairsCapital One · OACapital One interview questions
  • Maximum Equal Parts for PrefixesAmazon · OA
  • Cheating CodemakerJane Street · OAJane Street interview questions
  • Count Pairs Differing in One DigitRoblox · OARoblox interview questions
  • Candidate Alias Name MatchingCheckr · OA · PHONE SCREEN
  • Aggregate Paginated Project Run RecordsReplit · OA
  • Maximum Common Decimal Prefix Across ArraysZipRecruiter · OAZipRecruiter interview questions
  • Count User Mentions in Chat MessagesRamp · OARamp interview questions
  • Find Words That Can Be Formed by CharactersTabaPay · OA
  • Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions

TikTok interview experiences

  • TikTok AI Infra Intern LLM Round 2: 1 Experience
  • TikTok AI Infra Intern DeepSpeed Round: 1 Experience
  • TikTok AI Infra Intern Kernel Round: 1 Experience
  • TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
  • TikTok AI Infra Intern Interview Timeline: 1 Experience
  • TikTok AI Infra Intern Onsite Round 2: 1 Experience
All TikTok interview questionsSee TikTok hiring insights
Saved
class Solution {
    public long countSuffixPairs(String[] words) {
        // Write your code here.
    }
}
Checking account…