KatInterview

Check Password Compliance and Report Violations

StringsAuto-graded · 23 test cases
MediumAuto-gradedStringsvalidationstring-processingpattern-matching

Last updated

Tests string validation logic, character classification, case-insensitive pattern matching, and consecutive character counting.

You are building a password validator that checks a candidate password against five security rules. Your task is to identify every rule the password breaks and return them as a list.

Security Rules

A valid password must satisfy all of the following:

  1. Minimum length: The password must contain more than 15 characters (16 or more is required).
  2. Forbidden substring: The password must not include the word "password" as a substring, ignoring case. For instance, "MyPassWord" violates this rule.
  3. Mixed case requirement: The password must include at least one uppercase letter (A-Z) and at least one lowercase letter (a-z).
  4. Special character requirement: The password must contain at least one character from a provided set of exactly three special symbols.
  5. Repetition limit: No character may appear more than 4 times in a row. For example, "aaaaa" is invalid, but "aaaa" is acceptable.

Task

Write a function that accepts a password string and a list of three special characters, then returns a list of rule identifiers for every rule the password violates. Rules are identified by their labels: "rule_1", "rule_2", "rule_3", "rule_4", and "rule_5".

Return the violations in ascending rule order. If the password is fully compliant, return an empty list.

Constraints

  • 0 ≤ len(password) ≤ 200
  • The special characters list always contains exactly 3 distinct single-character strings
  • All characters in the password are printable ASCII
  • Rule 3 is reported once if either the uppercase or lowercase requirement (or both) is missing
  • Rule 5 checks for consecutive identical characters (case-sensitive)
  • Rule 2 performs a case-insensitive substring search

Examples

Example 1

Input: check_password_rules('MyPassWord123', ['!', '@', '#'])

Output: ['rule_1', 'rule_2', 'rule_4']

Example 2

Input: check_password_rules('StrongPassword!1', ['!', '@', '#'])

Output: ['rule_2']

Example 3

Input: check_password_rules('ValidPassword!Aa123', ['!', '@', '#'])

Output: ['rule_2']

Example 4

Input: check_password_rules('LongEnough!Aa12345', ['!', '@', '#'])

Output: []

Hints

Hint 1

Check each rule independently and collect all violations rather than stopping at the first failure.

Hint 2

For the consecutive character limit, track the current run length as you scan through the password and update the maximum run seen.

Hint 3

Remember that 'more than 15' means a password of exactly 15 characters is too short.