easy +8 pts

Validate username format

Check if a username meets common length, character, and concurrency rules.

Write a function `validate_username(username: str) -> bool` that returns `True` if the username is valid, and `False` otherwise. A username is valid if and only if all of the following conditions hold: 1. Length is between 3 and 16 characters inclusive. 2. It contains only lowercase letters (`a-z`), digits (`0-9`), and underscores (`_`). 3. It does not start or end with an underscore. 4. It does not contain two consecutive underscores. 5. It contains at least one letter and at least one digit. The function should work for any non-empty string. You can assume the input is always a string.

Constraints

The input string length is at most 100. The function must run in O(n) time and use O(1) extra space.

Example

>>> validate_username("user_123")
True
>>> validate_username("_user123")
False
>>> validate_username("user__123")
False
>>> validate_username("user123")
True
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Check length first, then iterate through characters to enforce character set and consecutive underscore rule.
Use flags to track whether you've seen at least one letter and at least one digit.
Remember to check the first and last characters explicitly.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.