Validate Password Against Multiple Rules
Company: Otter.Ai
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates string processing and input validation skills, specifically character-class checks, frequency constraints, and enforcement of multiple simultaneous rules for password strength.
Constraints
- 0 <= len(password) <= 10^5
- 0 <= min_length <= 10^5
- forbidden and special_chars contain single-character strings
- Checking membership in forbidden and special_chars should be treated as O(1)
Examples
Input: ("Abc!defG", 8, {" "}, {"!", "@", "#"})
Expected Output: True
Explanation: Length is exactly 8, it has uppercase and lowercase letters, includes '!', contains no forbidden space, and no character appears more than 4 times.
Input: ("abcd!efg", 8, {"$"}, {"!", "@", "#"})
Expected Output: False
Explanation: It has lowercase letters and a special character, but no uppercase English letter.
Input: ("ABCD!EFG", 8, {"$"}, {"!"})
Expected Output: False
Explanation: It has uppercase letters and a special character, but no lowercase English letter.
Input: ("Abcdefgh", 8, {"$"}, {"!", "@"})
Expected Output: False
Explanation: It meets the length and letter requirements, but contains no character from special_chars.
Input: ("Abc!!de ", 7, {" "}, {"!"})
Expected Output: False
Explanation: The password contains a forbidden space character, so it must be rejected.
Input: ("Aaaaaa!b", 8, {"$"}, {"!"})
Expected Output: False
Explanation: The lowercase character 'a' appears 5 times, which violates the maximum of 4 occurrences.
Input: ("", 1, {"$"}, {"!"})
Expected Output: False
Explanation: An empty password fails the minimum length requirement and also lacks required character categories.
Input: ("Aa!!bb!!", 8, {"$"}, {"!"})
Expected Output: True
Explanation: The character '!' appears exactly 4 times, which is allowed, and all other rules are satisfied.
Hints
- You can validate all rules except the length check in a single left-to-right pass over the password.
- Use a hash map for character frequencies and boolean flags for uppercase, lowercase, and special-character checks.