Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Validate User Input Function

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Validate User Input Function. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

The CoBank Customer Portal needs a lightweight validator for usernames before registration or profile updates. Implement a function that returns whether a proposed username satisfies all required formatting rules.

Formal Specification

Given a string username, return True if it is valid and False otherwise. A valid username must:

  1. Contain between 3 and 20 characters, inclusive.
  2. Start with an uppercase or lowercase English letter.
  3. Contain only English letters, digits, and underscores.
  4. Contain at least one character. The length requirement already excludes empty input.

Do not modify the input string. Treat uppercase and lowercase letters as valid without converting the username.

Constraints

  • 0 <= len(username) <= 10^5
  • 3 <= len(valid_username) <= 20
  • Input contains printable ASCII characters
  • Only letters, digits, and underscores are allowed
  • The first character must be an English letter

Function Signature

def is_valid_username(username):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output