How would you design and implement a password checker given certain requirements for the password?
Question Explain
This question can be broken down into a few key components:
-
You would need to design a password checker: Essentially, you are being asked to devise a strategy or blueprint of how you would structure a program or algorithm to check passwords against certain criteria.
-
You need to implement this design: After outlining your design, you are expected to translate this into code using a programming language of your choice.
-
The password checker should consider certain requirements for the password: Meaning, there must be stipulated criteria or rules the password has to conform to, for example, a minimum length, the inclusion of certain character types (uppercase, lowercase, digits, special characters, etc.) and any other constraints.
As such, your response should consider:
- Identifying the requirements for the password.
- The steps needed to compare the provided password against these requirements.
- Discussing any efficiency considerations if applicable.
- Writing a sample implementation of the password checker.
Answer Example 1
Given password requirements such as a minimum length of 8, at least one capital letter, at least one lower-case letter, and one special character, I'd design a password checker as follows:
- First, verify that the password meets the minimum length requirement.
- Then, iterate over each character in the password to ensure it meets the various character requirements.
And now for the implementation:
def password_checker(password):
if len(password) < 8:
return "Password is too short"
if not any(c.isupper() for c in password):
return "Password needs at least one uppercase letter"
if not any(c.islower() for c in password):
return "Password needs at least one lowercase letter"
if not any(c in "!@#$%^&*()" for c in password):
return "Password needs at least one special character"
return "Password is valid"
Notably, in this approach, the function verifies each requirement in a separate step, which makes the code simple and direct, though possibly running multiple loops over the password.
Answer Example 2
A more efficient version of this function could verify all the requirements in a single pass:
def password_checker(password):
min_length = 8
has_upper = has_lower = has_special = False
if len(password) < min_length:
return "Password is too short"
for c in password:
if c.isupper():
has_upper = True
elif c.islower():
has_lower = True
elif c in "!@#$%^&*()":
has_special = True
if not has_upper:
return "Password needs at least one uppercase letter"
if not has_lower:
return "Password needs at least one lowercase letter"
if not has_special:
return "Password needs at least one special character"
return "Password is valid"
This implementation ensures all conditions are checked within one loop through the password. It utilizes boolean flags for each condition and assigns them true once the condition is encountered. This not only meets the requirements but also takes efficiency into consideration.
More Questions
- How do you launch a product?
- You are a PM for the health team at Meta. Design a product for tissue donation (tissue = blood, kidney, bone marrow, or any body part).
- How do you double a new app’s user base in 6 months?
- What are three endpoints on Discord?
- How would you determine success of LinkedIn's Job feature?