The Password Forge

An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
easy
Seniority
L3
Asked in
Rally Health

Problem

Write is_strong_password(password) that returns True only if the password is a strong one, and False otherwise. A password is strong when ALL of these hold:

1. It is exactly 8 characters long. 2. It contains at least one uppercase letter (A-Z). 3. It contains at least one lowercase letter (a-z). 4. It contains at least one digit (0-9). 5. It contains at least one special character from the set !@#$%^&*.

Return a bool. For example, "Abcd1!@x" returns True, while "abcdefgh", "Abcd1234", and "Ab1!" all return False.

Does this password pass the strength gate?

Example 1

Input:

password = "Abcd1!@x"

Output:

true

Example 2

Input:

password = "abcdefgh"

Output:

false

Worked solution and explanation

Why this problem exists in real interviews

This tests constraint satisfaction over a string. It checks whether candidates can validate that several independent character-class requirements are all satisfied at once, combining str methods and membership tests with clean boolean logic.


Break down the requirements

Step 1: Check the length first

Before checking character classes, reject anything that is not exactly 8 characters. This is the cheapest, most disqualifying check.

Step 2: Confirm uppercase, lowercase, and digit are present

Scan the password once and confirm it contains at least one uppercase letter, one lowercase letter, and one digit. str.isupper(), str.islower(), and str.isdigit() on individual characters make this straightforward.

Step 3: Confirm a special character is present

Confirm at least one character is in the special set !@#$%^&*. A simple membership test against that string covers it.

Step 4: Combine every check

Return True only when the length and all four character-class checks pass; otherwise return False.


The solution

Length gate plus four character-class checks
def is_strong_password(password):
    if len(password) != 8:
        return False
    specials = '!@#$%^&*'
    has_upper = any(c.isupper() for c in password)
    has_lower = any(c.islower() for c in password)
    has_digit = any(c.isdigit() for c in password)
    has_special = any(c in specials for c in password)
    return has_upper and has_lower and has_digit and has_special

Common follow-up questions