All practice tasks

Coding (Algo)

Find the Shared Name Prefix

Boundary handling · Easy

A file-grouping tool suggests the beginning shared by every supplied name. It must preserve original spelling and may return an empty prefix. A name ending early limits the prefix even if all other names continue with matching characters.

Return the longest case-sensitive prefix shared by every supplied name without changing or reordering the names.

Input

Call

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

Input shape

{ words: string[] }

Fields

  • words is an array of names; empty names and duplicates are allowed.
  • Strings contain printable ASCII characters only. Comparisons are case-sensitive; spaces count as characters.

Output

Return value

  • Return a string prefix. An empty array returns an empty string; one name returns that entire name.
  • If any name is empty or the first characters disagree, return an empty string.

Constraints

  • 0 <= words.length <= 10000; the sum of all name lengths is at most 200000.

Examples

Example 1

{
  "words": [
    "service-api",
    "service-web",
    "service"
  ]
}

Returns

"service"

Example 2

{
  "words": [
    "App",
    "apple"
  ]
}

Returns

""

Required complexity

O(total input characters + number of words) time and O(1) auxiliary space excluding the returned prefix

What this task practices

Find the Shared Name Prefix 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 easy 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.