PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string processing and input validation skills, specifically character-class checks, frequency constraints, and enforcement of multiple simultaneous rules for password strength.

  • easy
  • Otter.Ai
  • Coding & Algorithms
  • Software Engineer

Validate Password Against Multiple Rules

Company: Otter.Ai

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Implement a password validation function for an account security system. You are given: - a string `password` - an integer `min_length` - a set of forbidden characters `forbidden` - a set of characters considered special, `special_chars` A password is accepted only if all of the following conditions are true: 1. Its length is at least `min_length`. 2. It does not contain any character from `forbidden`. 3. No character appears more than 4 times in the entire password. 4. It contains at least one uppercase English letter. 5. It contains at least one lowercase English letter. 6. It contains at least one character from `special_chars`. Return `true` if the password is valid and `false` otherwise. In addition to coding the solution, explain how you would check each rule, analyze the time and space complexity, and write representative unit tests for both valid and invalid passwords.

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.

Implement a function that returns True if a password satisfies all security rules and False otherwise. The password is valid only if: (1) its length is at least min_length, (2) it contains no character from forbidden, (3) no single character appears more than 4 times in the entire password, (4) it contains at least one uppercase English letter A-Z, (5) it contains at least one lowercase English letter a-z, and (6) it contains at least one character from special_chars. A good approach is to check the length first, then scan the password once: reject immediately on forbidden characters, maintain a frequency map to enforce the repetition rule, and track three flags for uppercase, lowercase, and special characters. The repetition rule is case-sensitive, so 'A' and 'a' count as different characters. Representative unit tests should include valid passwords, missing uppercase/lowercase/special cases, forbidden characters, repeated characters above the limit, exact boundary cases, and empty input.

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

  1. You can validate all rules except the length check in a single left-to-right pass over the password.
  2. Use a hash map for character frequencies and boolean flags for uppercase, lowercase, and special-character checks.
Last updated: May 25, 2026

Related Coding Questions

  • Evaluate Arithmetic Expressions - Otter.Ai (medium)
  • Implement an Arithmetic Expression Evaluator - Otter.Ai (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.