Skip to main content
EasyAlgorithms & Data StructuresBuild

Clean and Validate Usernames

User-supplied username strings often arrive with leading or trailing noise — symbols, punctuation, spaces — before the actual username. Your job is to extract the username by trimming the invalid edges, then validate wh...

What you will practice

PythonArrays & StringsTwo PointersStringsValidation

Requirements

  • Implement `extract_valid_usernames(raw_inputs: list[str]) -> list[str]` in `main.py`.
  • Use two pointers to strip leading and trailing characters that are not letters, digits, or underscores.
  • A candidate is valid only if every character in it is a letter, digit, or underscore.
  • A candidate is valid only if its length is between 3 and 20 characters (inclusive).
  • Return valid extracted usernames in their original order. Inputs that yield no valid candidate are excluded.
  • An empty input list returns an empty list.

Starter files

main.pyEditable starter

What the judge checks

  • Runs in the python environment with the python-pytest runner.
  • Uses a 5000ms judge budget.
  • Behavior rules include: Strips Leading Invalid Characters, Strips Trailing Invalid Characters, Validates Length 3 To 20, Rejects Invalid Middle Characters.

Constraints

  • Input strings may contain any printable characters.
  • `raw_inputs` may be empty.
  • Preserve the order of valid usernames as they appeared in the input.

Example behavior

Input
raw_inputs = ["@alice_99!", "  bob  ", "@ab!"]
Output
["alice_99", "bob"]

@alice_99! → trim @ and ! → "alice_99" (8 chars, all valid). " bob " → trim spaces → "bob" (3 chars, valid). @ab! → trim → "ab" (2 chars) — too short, excluded.

Input
raw_inputs = ["hello world", "alice-bob", "valid_name"]
Output
["valid_name"]

"hello world" has a space in the middle — not a valid character. "alice-bob" has a hyphen in the middle. "valid_name" passes all checks.

Follow-up

The current approach trims edges and then validates the middle. Could you combine those two phases into a single pass with a different pointer strategy?