All practice tasks

Coding (Algo)

Count Distinct Text Fragments

Suffix automata · Hard

A text-index experiment measures how many different contiguous fragments a string contains. Several ranges can spell the same fragment and should count only once. The input is large enough that materializing every substring is not a viable implementation; this is an explicitly specialist string-indexing exercise.

Return the number of distinct non-empty contiguous substrings without constructing or storing all of those substrings.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ text: string }

Fields

  • text is a case-sensitive printable ASCII string. A fragment is a consecutive sequence of characters, not a subsequence.

Output

Return value

  • Return one integer count. Different positions with identical text contribute one distinct fragment.
  • The empty fragment is excluded and empty input returns zero. Do not normalize spaces, case or punctuation.

Constraints

  • 0 <= text.length <= 200000; character codes are 32 through 126.
  • The answer is at most n*(n+1)/2 and fits a safe JSON integer. No external packages or network lookups are available.

Examples

Example 1

{
  "text": "aba"
}

Returns

5

Example 2

{
  "text": "aaaa"
}

Returns

4

Required complexity

O(n) expected time and O(n) space using bounded-alphabet suffix-state indexing

What this task practices

Count Distinct Text Fragments is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as hard difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.