Skip to content

ORIGINAL CODING QUESTION

Distinct skill-path prefixes

Each learning path is a sequence of skill tokens. A prefix is the first one or more tokens of a path. Return how many DIFFERENT nonempty prefixes appear across all paths; a prefix shared by several paths is counted once.

Open practice workspace

Original exercise · easy. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n paths. Each word is written as its length L followed by its L tokens.

Constraints

1 ≤ n ≤ 50; 1 ≤ L ≤ 20; every token is an integer in [1,26].

Example 1

Input

3 3 1 2 3 2 1 2 2 4 5

Output

5

The prefixes are (1), (1,2), (1,2,3), (4) and (4,5).

Example 2

Input

2 2 7 7 2 7 7

Output

2

Both paths are identical, giving only (7) and (7,7).

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Two paths that start the same way share those prefixes.

Hint 2

Insert every path into a trie, creating nodes only when missing.

Hint 3

Each trie node below the root is one distinct prefix.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →