Check Password Compliance and Report Violations
StringsAuto-graded · 23 test casesLast 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:
- Minimum length: The password must contain more than 15 characters (16 or more is required).
- Forbidden substring: The password must not include the word
"password"as a substring, ignoring case. For instance,"MyPassWord"violates this rule. - Mixed case requirement: The password must include at least one uppercase letter (A-Z) and at least one lowercase letter (a-z).
- Special character requirement: The password must contain at least one character from a provided set of exactly three special symbols.
- 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires validating a password against five independent rules and collecting all violations, not just the first one encountered.
The solution strategy is:
-
Check length directly (Rule 1): A simple length comparison determines if the password has more than 15 characters.
-
Search for forbidden substring (Rule 2): Convert the password to lowercase and check whether
"password"appears anywhere within it. Lowercasing both the search term and the string ensures case-insensitive matching. -
Single linear scan for remaining rules (Rules 3, 4, 5): Walk through the password once, maintaining:
- Boolean flags tracking whether an uppercase letter, lowercase letter, and special character have been seen
- The length of the current run of identical consecutive characters
- The maximum run length observed so far
-
Evaluate and collect violations: After completing the scan, check each rule's condition. If violated, append the corresponding rule identifier to the result list in order.
Why It Works
- Rule 1: Direct length comparison correctly identifies passwords with 15 or fewer characters.
- Rule 2: Python's
inoperator finds substrings, and converting to lowercase makes the search case-insensitive. - Rule 3: The flags start as
Falseand flip toTruewhen the first qualifying character is encountered. After the scan, both flags areTrueonly if both character types exist. - Rule 4: Converting the special character list to a set enables O(1) membership checking. The flag becomes
Trueif any password character matches. - Rule 5: For each character, if it matches the previous one, the run extends; otherwise, a new run begins. Tracking the maximum run throughout the entire scan (including runs that end at the last character) ensures we catch all violations. Checking
max_consecutive > 4after the loop identifies passwords with 5 or more consecutive identical characters.
Evaluating all rules independently and appending violations in rule-number order guarantees the output is complete, deterministic, and sorted.
Complexity
Time: O(n), where n is the length of the password. Each rule requires at most one pass through the string. The substring check for "password" is O(n) for a fixed 8-character pattern.
Space: O(n) for the lowercased copy in rule 2, plus O(1) for flags and counters. The special character set is O(1) since it always contains exactly 3 elements.
Solution
from typing import List
def check_password_rules(password: str, special_chars: List[str]) -> List[str]:
violations = []
special_set = set(special_chars)
# Rule 1: Length must be greater than 15
if len(password) <= 15:
violations.append("rule_1")
# Rule 2: Must not contain "password" (case-insensitive)
if "password" in password.lower():
violations.append("rule_2")
# Single pass for rules 3, 4, and 5
has_upper = False
has_lower = False
has_special = False
max_consecutive = 0
current_run = 0
prev_char = None
for char in password:
# Rule 3: Check for uppercase and lowercase
if 'A' <= char <= 'Z':
has_upper = True
elif 'a' <= char <= 'z':
has_lower = True
# Rule 4: Check for special characters
if char in special_set:
has_special = True
# Rule 5: Track consecutive identical characters
if char == prev_char:
current_run += 1
else:
current_run = 1
prev_char = char
max_consecutive = max(max_consecutive, current_run)
# Rule 3: Must have both uppercase and lowercase
if not has_upper or not has_lower:
violations.append("rule_3")
# Rule 4: Must have at least one special character
if not has_special:
violations.append("rule_4")
# Rule 5: No more than 4 consecutive identical characters
if max_consecutive > 4:
violations.append("rule_5")
return violations